扫一扫
分享文章到微信

扫一扫
关注官方公众号
至顶头条
请教一个case when的问题?
sql:
select count(ID) CNT,TYPEGROUP from( 
select ID, 
case when (COL_A>0 and COL_A <= 10) then 1 when (COL_A>10 and COL_A <=20)    then 2 
when (COL_A>20 and COL_A <=30)  then 3    when (COL_A>30 and COL_A <=40)  then 4 
when (COL_A>40 and COL_A <=50) then 5    when (COL_A>50 and COL_A <=60)  then 6 
when (COL_A>60 and COL_A <=70) then 7    when (COL_A>70 and COL_A <=80)  then 8 
when (COL_A>80 and COL_A <=90) then 9    when (COL_A>90 and COL_A <=100)  then 10 
when (COL_A>100 and COL_A <=110) then 11  when (COL_A>110 and COL_A <=120)  then 12 
when (COL_A>120 and COL_A <=130) then 13  when (COL_A>130) then 14 end TYPEGROUP 
from T_TEST 
) group by TYPEGROUP 
order by TYPEGROUP 
结果是这样的: 
CNT    TYPEGROUP 
11        1 
32        2 
15        3 
34        4 
32        6 
24        7 
53        8 
42        9 
我希望能把其中的5,10,11,12,13,14都显示出来,只不过CNT为空或0 
按这个思路应当可以的,只是没数据测试,楼主不妨试试:
select count(ID)-1 CNT,TYPEGROUP from( 
select ID, 
case when (COL_A>0 and COL_A <= 10) then 1 when (COL_A>10 and COL_A <=20)    then 2 
when (COL_A>20 and COL_A <=30)  then 3    when (COL_A>30 and COL_A <=40)  then 4 
when (COL_A>40 and COL_A <=50) then 5    when (COL_A>50 and COL_A <=60)  then 6 
when (COL_A>60 and COL_A <=70) then 7    when (COL_A>70 and COL_A <=80)  then 8 
when (COL_A>80 and COL_A <=90) then 9    when (COL_A>90 and COL_A <=100)  then 10 
when (COL_A>100 and COL_A <=110) then 11  when (COL_A>110 and COL_A <=120)  then 12 
when (COL_A>120 and COL_A <=130) then 13  when (COL_A>130) then 14 end TYPEGROUP 
from T_TEST 
union all (select 1, level from dual connect by level <15) 
) group by TYPEGROUP 
order by TYPEGROUP 
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。