select 类型=LX
,一队=sum(case QX when '一队' then 1 else 0 end)
,二队=sum(case QX when '二队' then 1 else 0 end)
,三队=sum(case QX when '三队' then 1 else 0 end)
,合计=count(*)
from 表A group by LX

解决方案 »

  1.   

    declare @s varchar(8000)set @s=''
    select @s=@s+',['+QX+']=sum(case QX when '''+QX+''' then 1 else 0 end)'
    from 表
    group by QXexec('select LX as 类型"+@s+' 
          from 表
          group by LX ')
      

  2.   


    --如果QX是不固定的,则用
    declare @s varchar(8000)
    set @s='select 类型=LX'
    select @s=@s+',['+rtrim(QX)+']=sum(case QX when '''+rtrim(QX)+''' then 1 else 0 end)'
    from 表A group by QX
    exec(@s+'
    ,合计=count(*)
    from 表A group by LX')
      

  3.   

    少写了一个合计 
    declare @s varchar(8000)set @s=''
    select @s=@s+',['+QX+']=sum(case QX when '''+QX+''' then 1 else 0 end)'
    from 表
    group by QXexec('select LX as 类型"+@s+',count(*) as 合计 
          from 表
          group by LX ')
      

  4.   

    select 类型=LX
    ,一队=sum(case QX when '一队' then 1 else 0 end)
    ,二队=sum(case QX when '二队' then 1 else 0 end)
    ,三队=sum(case QX when '三队' then 1 else 0 end)
    ,合计=count(*)
    from 表A group by LX
    UNion all
    select 类型='合计',
            ,一队=sum(case QX when '一队' then 1 else 0 end)
    ,二队=sum(case QX when '二队' then 1 else 0 end)
    ,三队=sum(case QX when '三队' then 1 else 0 end)
    from 表A
      

  5.   

    select 类型=LX,一队=sum(case QX when '一队' then 1 else 0 end),二队=sum(case X when '二队' then 1 else 0 end),三队=sum(case QX when '三队' then 1 else 0 end) ,合计=count(*)from 表A group by LX
      

  6.   

    --少了最后的合计,改一下:select 类型=case when grouping(LX)=1 then '合计' else LX end
    ,一队=sum(case QX when '一队' then 1 else 0 end)
    ,二队=sum(case QX when '二队' then 1 else 0 end)
    ,三队=sum(case QX when '三队' then 1 else 0 end)
    ,合计=count(*)
    from 表A group by LX with rollup
    --如果QX是不固定的,则用
    declare @s varchar(8000)
    set @s='select 类型=case when grouping(LX)=1 then ''合计'' else LX end'
    select @s=@s+',['+rtrim(QX)+']=sum(case QX when '''+rtrim(QX)+''' then 1 else 0 end)'
    from 表A group by QX
    exec(@s+'
    ,合计=count(*)
    from 表A group by LX with rollup')
      

  7.   

    select 类型=LX
    ,一队=sum(case QX when '一队' then 1 else 0 end)
    ,二队=sum(case QX when '二队' then 1 else 0 end)
    ,三队=sum(case QX when '三队' then 1 else 0 end)
    ,合计=count(*)
    from 表A group by LX
      

  8.   

    count(*) 是所有 怎么返回只有 汽车一队 汽车二队 三队的?
      

  9.   

    : zjcxc(邹建) 
    --如果QX是不固定的,则用
    declare @s varchar(8000)
    set @s='select 类型=case when grouping(LX)=1 then ''合计'' else LX end'
    select @s=@s+',['+rtrim(QX)+']=sum(case QX when '''+rtrim(QX)+''' then 1 else 0 end)'
    from 表A group by QX
    exec(@s+'
    ,合计=count(*)
    from 表A group by LX with rollup')group by LX 是否应改为group by case when grouping(LX)=1 then ''合计'' else LX end
      

  10.   

    要不然 “类型=case when grouping(LX)=1 then ''合计'' else LX end'”这句没有聚合函数应该不行吧?zjcxc(邹建) 大师希望得到你的答复。
      

  11.   

    --测试--测试数据
    create table 表A(LX varchar(10),QX varchar(10))
    insert 表A select 'a','一队'
    union  all select 'b','二队'
    union  all select 'c','三队'
    union  all select 'c','一队'
    union  all select 'a','二队'
    union  all select 'b','三队'
    union  all select 'c','一队'
    union  all select 'a','二队'
    union  all select 'b','三队'
    union  all select 'a','一队'
    union  all select 'b','二队'
    go--QX固定的情况
    select 类型=case when grouping(LX)=1 then '合计' else LX end
    ,一队=sum(case QX when '一队' then 1 else 0 end)
    ,二队=sum(case QX when '二队' then 1 else 0 end)
    ,三队=sum(case QX when '三队' then 1 else 0 end)
    ,合计=count(*)
    from 表A group by LX with rollup
    --如果QX是不固定的,则用
    declare @s varchar(8000)
    set @s='select 类型=case when grouping(LX)=1 then ''合计'' else LX end'
    select @s=@s+',['+rtrim(QX)+']=sum(case QX when '''+rtrim(QX)+''' then 1 else 0 end)'
    from 表A group by QX
    exec(@s+'
    ,合计=count(*)
    from 表A group by LX with rollup')
    go--删除测试
    drop table 表A/*--测试结果类型         一队          二队          三队          合计          
    ---------- ----------- ----------- ----------- ----------- 
    a          2           2           0           4
    b          0           2           2           4
    c          2           0           1           3
    合计         4           4           3           11(所影响的行数为 4 行)
    类型         二队          三队          一队          合计          
    ---------- ----------- ----------- ----------- ----------- 
    a          2           0           2           4
    b          2           2           0           4
    c          0           1           2           3
    合计         4           3           4           11--*/
      

  12.   

    to: fzc20031210(hnytnet) 你自己去测试了再说吧