有一张数据表如下:(0为空卡交易)
商号 交易金额  交易日期
1511710  27.9 2011-10-3
1511710  0 2011-10-3
1511710  60 2011-10-12
1511710  0 2011-10-13
1511710  69.1 2011-10-21
1511710  235 2011-10-23
1511710  105 2011-10-30
1512833  100 2011-10-11
1512833  37.5 2011-10-15
1512833  0 2011-10-15
1512833  118 2011-10-20
1512833  95.3 2011-10-21
1512833  49.2 2011-10-25
1513545  105 2011-10-2
1513545  200 2011-10-5
.......
我想统计每年底各个商号的交易汇总,如下格式,应该怎么写这个语句。
商号 交易笔数  交易金额  空卡笔数
1511710   7  497 2
1512833   6  400 1
1513545   2  305 0
......
只统计交易笔数和金额没有问题,可是要区分出空卡,我就不知道怎么写了,请各位大虾帮帮忙啊。

解决方案 »

  1.   

    select 商号,count(商号) 交易笔数,sum(交易金额) 交易金额,
    sum(decode(交易金额,0,1,0)) 空卡笔数
    from table_test group by 商号
      

  2.   

    可以添加子查询查出空卡数量select t.商号,count(t.商号) 商号,sum(t.交易金额) 交易金额,
          (select count(商号) from tb1 where 交易金额=0 and 商号=t.商号 group by 商号) 空卡笔数 
    from tb1 t
    group by 商号
    order by 商号
      

  3.   


    select t.商号,count(t.商号) 商号,sum(t.交易金额) 交易金额, 
            (select count(商号) from tb1 
                 where 交易金额=0 and 商号=t.商号 and 
                 to_char(交易日期,'yyyy')=2011 group by 商号) 空卡笔数   
    from tb1 t 
    where to_char(交易日期,'yyyy')=2011
    group by 商号 
    order by 商号 
      

  4.   


    select 商号,count(商号) 交易笔数,sum(交易金额) 交易金额,
    sum(decode(交易金额,0,1,0)) 空卡笔数
    from table_test group by 商号