我希望将以下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发布于 2014-10-28 07:05:06
作为张荣佳回答的另一种选择:1)要获得常量值,只需使用字符串常量而不是cloumn。2.)您可以使用union将查询连接到一个查询中。
看起来是这样的:
select 'night', count(incnum) as incidentcout from kincident where (to_char(reportdate,'hh24') between 0 and 2.59 or to_char(reportdate,'hh24') between 21 and 23.59
union
select 'afternoon', count(incnum) as incidentcout from kincident where to_char(reportdate,'hh24') between 15 and 20.59
union
select 'noon', count(incnum) as incidentcout as counttenth1 from kincident where to_char(reportdate,'hh24') between 9 and 14.59
union
select 'morning', count(incnum) as incidentcout from kincident where to_char(reportdate,'hh24') between 3 and 8.59https://stackoverflow.com/questions/26602437
复制相似问题