在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