select good=sum(case value when 'good' then 1 else 0 end)
,better=sum(case value when 'better' then 1 else 0 end)
,best=sum(case value when 'best' then 1 else 0 end)
from 表

解决方案 »

  1.   

    如果不用CASE,用union怎么办呢.
      

  2.   

    union 是纵向合并的,不是横向合并的.
      

  3.   

    union可以纵向取得3个值,怎么才能认他横向排列?
    昨天我见到一个SQL语句就是用union解决的,只是我没有记住是怎么写的。
      

  4.   

    zjcxc(邹建)大哥的那种不好吗?
    我觉得很好啊!如果用union直接用是不可以实现的!它只能纵向接!
      

  5.   

    如果事先不知道value列具体有哪些不同值呢,假如还有一个bad的值呢
      

  6.   

    --value值不确定的话,标准的一句是写不出来的--取巧的一句还可以exec('set nocount on
    declare @s varchar(8000)
    set @s=''''
    select @s=@s+'',[''+rtrim(value)+'']=sum(case value when ''''''+rtrim(value)+'''''' then 1 else 0 end)''
    from 表 group by value
    set @s=stuff(@s,1,1,'''')
    exec(''select ''+@s+'' from 表'')')
      

  7.   

    如上所说,在程序中可以控制的:select distinct value from 表
    ……
    do while xxx.eof=false
       strsql=strsql+xxx.fields(0)+"……"
       xxx.movenext
    loop
      

  8.   

    --测试--测试数据
    create table 表(id int,value varchar(10))
    insert 表 select 1,'good'
    union all select 2,'good'
    union all select 3,'better'
    union all select 4,'best'
    union all select 5,'good'
    union all select 6,'better'
    go--取巧的一句
    exec('set nocount on
    declare @s varchar(8000)
    set @s=''''
    select @s=@s+'',[''+rtrim(value)+'']=sum(case value when ''''''+rtrim(value)+'''''' then 1 else 0 end)''
    from 表 group by value
    set @s=stuff(@s,1,1,'''')
    exec(''select ''+@s+'' from 表'')')
    go--删除测试
    drop table 表/*--测试结果best        better      good        
    ----------- ----------- ----------- 
    1           2           3
    --*/
      

  9.   

    to: rouqu(石林#黄果树) 没有测试通过是什么意思? 你看我上面的测试,有问题么?
      

  10.   

    参考前面的思想:
    declare @s varchar(800),@str varchar(100)
    set @str='(select count(*) from abc where value='
    set @s=''
    select @s=@s+','+value+'='+@str+''''+value+''''+')' from abc group by value
    select @s=stuff (@s,1,1,'')
    exec ('select '+@s)
      

  11.   

    对于确定的value,我有个sql:select sum(decode(value,'good',a)) as good,
           sum(decode(value,'better',a)) as better,
           sum(decode(value,'best',a)) as best 
    from (select count(*) as a,value from test.hzwtest group by value) 不过想想看,没有 zjcxc(邹建) 兄弟的好。
    我觉得他的方法应该是标准的行转列一句话的方法了。我的sql从本质上和他的一样,只不过复杂了很多:)
    pfpf
      

  12.   

    select good=sum(case value when 'good' then 1 else 0 end)
    ,better=sum(case value when 'better' then 1 else 0 end)
    ,best=sum(case value when 'best' then 1 else 0 end)
    from 表
    这样很好啊,又简洁又明了!
      

  13.   

    select good=sum(case value when 'good' then 1 else 0 end),better=sum(case value when 'better' then 1 else 0 end),best=sum(case value when 'best' then 1 else 0 end)
    from yourtable
      

  14.   

    declare @a table(id int,value varchar(10))
    insert @a select 1,'good'
    union all select 2,'good'
    union all select 3,'better'
    union all select 4,'best'
    union all select 5,'good'
    union all select 6,'better'select count(case when value='good' then id end)  as good
           ,count(case when value='better' then id end) as better
    ,count(case when value='best' then id end ) as best
    from @a
      

  15.   

    ---------------------------------------------**************-----------------------------------------------------------
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[table1]
    create table table1(id int  not null identity  primary key ,value varchar(20)  not  null)goinsert into table1
    select 'good'  union all
    select 'good'  union all
    select 'better'  union all
    select 'best'  union all
    select 'good'  union all
    select 'better'  
    goselect * from table1
    /*
    id       value  
    1 good
    2 good
    3 better
    4 best
    5 good
    6 better*/
    ----------方法一-------
    select sum(good) good,sum(better) better,sum(best) best from
    (
    select good=count(1),better=0,best=0 from table1 where value='good' 
    union all 
    select good=0,better=count(1),best=0 from table1 where value='better' 
    union all 
    select good=0,better=0,best=count(1) from table1 where value='best' 
    )a
    /*
    good     better   best
    3 2 1*/
    ----------方法二-------
    select good=sum(case value when 'good' then 1 end),better=sum(case value when 'better' then 1 end),best=sum(case value when 'best' then 1 end) from table1
    /*
    good     better   best
    3 2 1*/