我想合并以下SQL语句,以便将所有结果都放在一列中,而不是在4个单独的列中:
select count(incnum) as counttenth3 from kincident where (to_char(reportdate,'hh24') between 0 and 2.59 or to_char(reportdate,'hh24') between 21 and 23.59
select count(incnum) as counttenth2 from kincident where to_char(reportdate,'hh24') between 15 and 20.59
select count(incnum) as counttenth1 from kincident where to_char(reportdate,'hh24') between 9 and 14.59
select count(incnum) as counttenth0 from kincident where to_char(reportdate,'hh24') between 3 and 8.59
唯一的区别是为每个语句指定的时间范围。因此,我试图将它们全部合并为一列,并且我希望第二列包含具有给定字符串的行(不是来自数据库)
例如
Timing | count of incidents
-----------------------------
morning | 26
afternoon | 35
night | 40
像这样-未经测试,因为我通常写MS SQL :)
select
t.time_of_day,
count(t.time_of_day)
from
(
---
select
case to_char(reportdate,'hh24')
when between 15 and 20.59 then 'afternoon'
when between 9 and 14.59 then 'morning'
when between 3 and 8.59 then 'early-morning'
else 'night' --when between 0 and 2.59 or to_char(reportdate,'hh24') between 21 and 23.59
end as time_of_day
from
kincident
) t
group by
t.time_of_day
使用CASE语句对您的时间范围进行分类,我已将其放入子查询中,但是您可以将其包装在视图中;作为外部查询的一部分,我们对时间类别进行分组,然后在外部选择中进行计数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句