select  count(1),am.col1 from aa_mobile_yuanah5 am left join shortmessage_bak sm on am.col1=sm.mobile where opdate >date'2010-05-01' and opdate<date'2011-05-01'  group by am.col1  order by to_number(col2)
我是做短信上行发送的统计,统计出在某一时间段,指定的几千个手机号码上行发送的短信数量,我上面的写法出现了这样的问题如果这个用户在这一段时间没有上行不显示0,却直接不显示出来。请教各位高手。怎么写

解决方案 »

  1.   


    select am.col1,count(1) 
    from aa_mobile_yuanah5 am left join shortmessage_bak sm 
    on am.col1=sm.mobile 
    where opdate>date'2010-05-01' and opdate<date'2011-05-01' 
    group by am.col1 
    order by 1   
    /*
    oracle里to_number()貌似是转换为整型,而手机号都是11位,没必要转,直接字符排序跟数字排是一样的
    所以你要按手机号排要么就order by 1,要么就order by am.col1。其他的话这句子没别的区别了
      

  2.   

    --1
    select nvl(count(1),0),am.col1 from aa_mobile_yuanah5 am 
    left join shortmessage_bak sm 
    on am.col1=sm.mobile where opdate >date'2010-05-01' and opdate<date'2011-05-01' 
    group by am.col1 order by to_number(col2)--2如果没有的要显示为0的话,需要使用一个表,然后进行左连接。
    select m.*,nvl(n.cnt,0) cnt , n.col1 from 
    (含有所有用户的表) m left join
    (
    select count(1) cnt,am.col1 from aa_mobile_yuanah5 am 
    left join shortmessage_bak sm 
    on am.col1=sm.mobile where opdate >date'2010-05-01' and opdate<date'2011-05-01' 
    group by am.col1 order by to_number(col2)
    ) n
    on m.col1 = n.col1