按分数进行分组,并将null转换为0,同时显示出所有列

在sql中将为null显示为0,分组进行展示出来(类似行转列)

  • ORCAL中增加排序
select c ,sum(co)
from
(SELECT case
  when t.score < 60 then '60分以下'
  when t.score >= 60 and t.score < 70 then  '60-70'
  when t.score >= 70 and t.score < 80 then  '70-80'
  when t.score >= 80 and t.score < 90 then '80-90'
  when t.score >= 90 then '90分以上' end as c,
   COUNT(t.score) as co
from test_temp t
group by 
case
  when t.score < 60 then '60分以下'
  when t.score >= 60 and t.score < 70 then  '60-70'
  when t.score >= 70 and t.score < 80 then  '70-80'
  when t.score >= 80 and t.score < 90 then '80-90'
  when t.score >= 90 then '90分以上' end  
UNION SELECT  '60分以下'  as c,0 as co  from dual 
UNION SELECT '60-70' as c,0 as co  from dual 
UNION SELECT '70-80'  as c,0 as co  from dual
UNION SELECT '80-90' as c,0 as co  from dual
UNION SELECT  '90分以上' as c,0  as co  from dual ) 
group by c  order by  decode(c, '60分以下',1,'60-70',2, '70-80',3,'80-90',4,'90分以上',5)

输入出结果

输出结果

  • mysql中

```sql
select c ,sum(co)
from
(SELECT case
  when t.score < 60 then '60分以下'
  when t.score >= 60 and t.score < 70 then  '60-70'
  when t.score >= 70 and t.score < 80 then  '70-80'
  when t.score >= 80 and t.score < 90 then '80-90'
  when t.score >= 90 then '90分以上' end as c,
   COUNT(t.score) as co
from test_temp t
group by 
case
  when t.score < 60 then '60分以下'
  when t.score >= 60 and t.score < 70 then  '60-70'
  when t.score >= 70 and t.score < 80 then  '70-80'
  when t.score >= 80 and t.score < 90 then '80-90'
  when t.score >= 90 then '90分以上' end  
UNION SELECT  '60分以下'  as c,0 as co  from dual 
UNION SELECT '60-70' as c,0 as co  from dual 
UNION SELECT '70-80'  as c,0 as co  from dual
UNION SELECT '80-90' as c,0 as co  from dual
UNION SELECT  '90分以上' as c,0  as co  from dual ) 
group by c 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值