测试数据:
create table tttttt (nianyueday int,one money,two money,three money,four money)  insert tttttt select 20041001 , 100.00 ,    0   ,     0  ,   0
union all
select 20041001  ,200.00  ,  100.00,    0  ,   0
union all
select 20041001   ,  0  ,    200.00,  10.00 ,  0
union all
select 20041001 , 100.00  ,  0  ,       0  ,   20.00
union all
select 20041001 , 100.00 ,   0    ,    20.00 , 0
所求语句
select * from (select 'one' as 名称, count(*) as 次数,sum(one) 金额 from tttttt where isnull(one,0)>0 
union all
select 'two' as 名称, count(*) as 次数,sum(two) 金额 from tttttt where isnull(two,0)>0
union all
select  'three' as 名称,count(*) as 次数,sum(three) 金额 from tttttt where isnull(three,0)>0
union all
select  'four' as 名称,count(*) as 次数,sum(four) 金额 from tttttt where isnull(four,0)>0 ) b

解决方案 »

  1.   

    select 'One' as 名次,count(*) as 次数,sum(one) as 金额 from Table1
    where nianyueday='20041001' and one<>0
    union all
    select 'two' as 名次,count(*) as 次数,sum(two) as 金额 from Table1
    where nianyueday='20041001' and two<>0
    union all
    select 'three' as 名次,count(*) as 次数,sum(three) as 金额 from Table1
    where nianyueday='20041001' and three<>0
    union all
    select 'four' as 名次,count(*) as 次数,sum(four) as 金额 from Table1
    where nianyueday='20041001' and four<>0
      

  2.   

    select 'One' as 名次,count(*) as 次数,sum(one) as 金额 from Table1 where nianyueday='20041001' and isnull(one,0)<>0
    union all
    select 'two',count(*),sum(two) from Table1 where nianyueday='20041001' and isnull(two,0)<>0
    union all
    select 'three' ,count(*),sum(three) from Table1 where nianyueday='20041001' and isnull(three,0)<>0
    union all
    select 'four',count(*) ,sum(four)  from Table1 where nianyueday='20041001' and snull(four,0)<>0
      

  3.   

    select 'One' as 名次,count(*) as 次数,sum(one) as 金额 from 你的表名 where nianyueday='20041001' and isnull(one,0)<>0
    union all
    select 'two',count(*),sum(two) from 你的表名 where nianyueday='20041001' and isnull(two,0)<>0
    union all
    select 'three' ,count(*),sum(three) from 你的表名 where nianyueday='20041001' and isnull(three,0)<>0
    union all
    select 'four',count(*) ,sum(four)  from 你的表名 where nianyueday='20041001' and isnull(four,0)<>0
      

  4.   

    --测试数据
    create table Table1(nianyueday int,one money,two money,three money,four money)
    insert Table1 
    select 20041001,100.00,0     ,0    ,0     union all
    select 20041001,200.00,100.00,0    ,0     union all
    select 20041001,0     ,200.00,10.00,0     union all
    select 20041001,100.00,0     ,0    ,20.00 union all
    select 20041001,100.00,0     ,20.00,0 
    go--查询
    select 名称='One',名次=count(*),金额=sum(One) 
    from Table1 
    where nianyueday=20041001 and isnull(One,0)>0
    union all
    select 名称='two',名次=count(*),金额=sum(two) 
    from Table1 
    where nianyueday=20041001 and isnull(two,0)>0
    union all
    select 名称='three',名次=count(*),金额=sum(three) 
    from Table1 
    where nianyueday=20041001 and isnull(three,0)>0
    union all
    select 名称='four',名次=count(*),金额=sum(four) 
    from Table1 
    where nianyueday=20041001 and isnull(four,0)>0
    order by 名次 desc
    go--删除测试
    drop table Table1/*--测试结果名称    名次          金额                    
    ----- ----------- --------------------- 
    One   4           500.0000
    three 2           30.0000
    two   2           300.0000
    four  1           20.0000(所影响的行数为 4 行)
    --*/
      

  5.   

    --字段名写错,是次数select 名称='One',次数=count(*),金额=sum(One) 
    from Table1 
    where nianyueday=20041001 and isnull(One,0)>0
    union all
    select 名称='two',次数=count(*),金额=sum(two) 
    from Table1 
    where nianyueday=20041001 and isnull(two,0)>0
    union all
    select 名称='three',次数=count(*),金额=sum(three) 
    from Table1 
    where nianyueday=20041001 and isnull(three,0)>0
    union all
    select 名称='four',次数=count(*),金额=sum(four) 
    from Table1 
    where nianyueday=20041001 and isnull(four,0)>0
    order by 次数 desc
      

  6.   

    select 名称='One',名次=count(*),金额=sum(One) 
    from Table1 
    where nianyueday=20041001 and isnull(One,0)>0
    union all
    select 名称='two',名次=count(*),金额=sum(two) 
    from Table1 
    where nianyueday=20041001 and isnull(two,0)>0
    union all
    select 名称='three',名次=count(*),金额=sum(three) 
    from Table1 
    where nianyueday=20041001 and isnull(three,0)>0
    union all
    select 名称='four',名次=count(*),金额=sum(four) 
    from Table1 
    where nianyueday=20041001 and isnull(four,0)>0
    order by 名次 desc