在我想要一个计数之后,我做了一个与group by的节

时间:2014-05-20 15:39:07

标签: sql count case-when

我做下一个查询:

 select case when anum like '0%' then 0 else 1 end anum ,
    ,voucher
    , date 
 from ledger
 group by voucher, date, case when accountnum like '0%' then 0 else 1 end
 order by voucher

之后我需要在一个查询中计算anum。

答案在这里: 我查询上面写的内容。然后我得到以下结果:

anum  voucher   date
1     1         2012-09-30
29    1         2012-09-30
2     101       2012-11-01
2     1131_3    2012-01-01
4     1131_11   2012-05-22
4     1131_11   2012-05-22

但我想看到这个:

anum  voucher   transdate
2     1         2012-09-30
1     101       2012-11-01
1     1131_3    2012-01-01
2     1131_11   2012-05-22

1 个答案:

答案 0 :(得分:0)

只需将其放在子查询中:

select count(*) as anum, voucher, date
from (
     select case when anum like '0%' then 0 else 1 end anum ,
        ,voucher
        , date 
     from ledger
     group by voucher, date, case when accountnum like '0%' then 0 else 1 end
     order by voucher
) sq
group by voucher, date