扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
请教一个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领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者