当前位置: 代码迷 >> Oracle开发 >> 怎么在统计查询中显示结果为空的记录
  详细解决方案

怎么在统计查询中显示结果为空的记录

热度:103   发布时间:2016-04-24 07:54:20.0
如何在统计查询中显示结果为空的记录?
例子如下
数据库中“第二季度”由于没有数据,因此进行统计查询时是不显示的,有什么办法能够让查询结果实现以下的转换?
就是在没有数据的情况下,也能显示所有的季度记录

  季度                   数量
第一季度               12
第三季度               16  
第四季度               7


-------------
  季度                   数量
第一季度               12
第二季度               0
第三季度               16  
第四季度               7

------解决方案--------------------
可以在结果上加UNION ALL:
select season, sum(num)
from
(
select season,count(*) as num from table
group by season
union all
select '第一季度 ' as season,0 as num from dual
union all
select '第二季度 ' as season,0 as num from dual
union all
select '第三季度 ' as season,0 as num from dual
union all
select '第四季度 ' as season,0 as num from dual
)
group by season
  相关解决方案