在SQLServer中执行下面的语句:
select case 
  when Ratedn_Max<1024 then '<1M'
  when Ratedn_Max>=1024 and Ratedn_Max<2048 then '1M-2M'
  when Ratedn_Max>=2048 and Ratedn_Max<3072 then '2M-3M'
  when Ratedn_Max>=3072 and Ratedn_Max<4096 then '3M-4M'
  when Ratedn_Max>=4096 and Ratedn_Max<5120 then '4M-5M'
  when Ratedn_Max>=5120 and Ratedn_Max<6144 then '5M-6M'
  when Ratedn_Max>=6144 and Ratedn_Max<7168 then '6M-7M'
  when Ratedn_Max>=7168 then '>7M'
end as maxrate
, count(Ratedn_Max) as line_count
from ccatstep_tbl_montst_statisc a,CCATSTEP_TBL_REP_ENDOFFICE b
where a.office_id = '001'  and trunk_id = '003-49Z' and cabinet_id = '01003-043J'
group by  case 
  when Ratedn_Max<1024 then '<1M'
  when Ratedn_Max>=1024 and Ratedn_Max<2048 then '1M-2M'
  when Ratedn_Max>=2048 and Ratedn_Max<3072 then '2M-3M'
  when Ratedn_Max>=3072 and Ratedn_Max<4096 then '3M-4M'
  when Ratedn_Max>=4096 and Ratedn_Max<5120 then '4M-5M'
  when Ratedn_Max>=5120 and Ratedn_Max<6144 then '5M-6M'
  when Ratedn_Max>=6144 and Ratedn_Max<7168 then '6M-7M'
  when Ratedn_Max>=7168 then '>7M' end
得到结果:
MAXRATE LINE_COUNT
1M-2M 28
2M-3M 168
5M-6M 7
6M-7M 7
>7M 63
我想得到如果LINE_COUNT是0的也要显示出来,这个如何实现?
既需要得到新的结果:
MAXRATE LINE_COUNT
1M-2M 28
2M-3M 168
3M-4M    0
5M-6M 7
6M-7M 7
>7M 63请高手帮忙看看。

解决方案 »

  1.   

    更正下,得到结果是
    既需要得到新的结果:
    MAXRATE LINE_COUNT
    <1M      0                --先前忘记把这条写上
    1M-2M 28
    2M-3M 168
    3M-4M    0
    5M-6M 7
    6M-7M 7
    >7M 63
      

  2.   

    再次更正下,得到结果是
    既需要得到新的结果:
    MAXRATE LINE_COUNT
    <1M      0                
    1M-2M 28
    2M-3M 168
    3M-4M    0
    4M-5M    0                --先前又忘记把这条写上
    5M-6M 7
    6M-7M 7
    >7M 63
      

  3.   

    增加一张表
    MAXRATE
    1M-2M
    2M-3M
    3M-4M
    5M-6M
    6M-7M
    >7M你上面的结果right join 这张表。
      

  4.   

    MAXRATE
    <1M               
    1M-2M
    2M-3M
    3M-4M    
    4M-5M               --先前又忘记把这条写上
    5M-6M
    6M-7M
    >7M
      

  5.   

    select '1M-2M' [MAXRATE],
    sum(case when Ratedn_Max>=1024 and Ratedn_Max<2048 then count(Ratedn_Max) else 0 end ) [line_count]
    from ccatstep_tbl_montst_statisc a,CCATSTEP_TBL_REP_ENDOFFICE b
    where a.office_id = '001'  and trunk_id = '003-49Z' and cabinet_id = '01003-043J'
    union all
    select '2M-3M' [MAXRATE],
    sum(case when Ratedn_Max>=2048 and Ratedn_Max<3072 then count(Ratedn_Max) else 0 end ) [line_count]
    from ccatstep_tbl_montst_statisc a,CCATSTEP_TBL_REP_ENDOFFICE b
    where a.office_id = '001'  and trunk_id = '003-49Z' and cabinet_id = '01003-043J'以此类推,可能动态语句好一些。
      

  6.   

    同意 achongsky(灵魂) ( ) 信誉:99    Blog  2006-08-31 15:53:00  得分: 0  
     
    的说法
      

  7.   

    create table aaa
    (MAXRATE varchar(10),
    LINE_COUNT int
    )
    insert aaa
    select '1M-2M', 28 union all
    select '2M-3M', 168 union all
    select '5M-6M', 7 union all
    select '6M-7M', 7 union all
    select '>7M', 63
    select * from aaa
    create table bbb
    (
    MAXRATE varchar(10)
    )
    i
    nsert bbb
    select '<1M'   union all                 
    select '1M-2M' union all        
    select '2M-3M'  union all        
    select '3M-4M'    union all        
    select '4M-5M'   union all        
    select '5M-6M' union all        
    select '6M-7M' union all        
    select '>7M'        
    select * from bbbselect bbb.maxrate,isnull(line_count,0) as line_count from bbb left join aaa on bbb.maxrate=aaa.maxrate
    maxrate    line_count  
    ---------- ----------- 
    <1M        0
    1M-2M      28
    2M-3M      168
    3M-4M      0
    4M-5M      0
    5M-6M      7
    6M-7M      7
    >7M        63