首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >组合SQL语句并包含用于时间范围的字符串

组合SQL语句并包含用于时间范围的字符串
EN

Stack Overflow用户
提问于 2014-10-28 06:53:30
回答 4查看 60关注 0票数 1

我希望将以下SQL语句组合起来,以便将所有结果放在一列中,而不是在4个单独的列中:

代码语言:javascript
运行
复制
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

唯一的区别是给出了每个语句的时间范围。因此,我试图将它们合并为一列,并希望第二列包含具有给定字符串的行(而不是数据库中的行)。

例如。

代码语言:javascript
运行
复制
Timing    |  count of incidents
-----------------------------
morning   | 26
afternoon | 35
night     | 40
EN

Stack Overflow用户

发布于 2014-10-28 07:05:06

作为张荣佳回答的另一种选择:1)要获得常量值,只需使用字符串常量而不是cloumn。2.)您可以使用union将查询连接到一个查询中。

看起来是这样的:

代码语言:javascript
运行
复制
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.59
票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26602437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档