Sql 从特定时间之间的多天(跨越2天)中选择数据

Sql 从特定时间之间的多天(跨越2天)中选择数据,sql,oracle,date,timestamp,Sql,Oracle,Date,Timestamp,我需要知道过去7天我的数据库中出现了多少个时间戳在23:00和01:00之间的条目 我遇到的问题是时间戳跨越了2天,并且不确定在一个查询中是否可能出现这种情况 到目前为止,我已经得出以下结论: select trunc(timestamp) as DTE, extract(hour from timestamp) as HR, count(COLUMN) as Total from TABLE where trunc(timestamp) >= '12-NOV-19' and

我需要知道过去7天我的数据库中出现了多少个时间戳在23:00和01:00之间的条目

我遇到的问题是时间戳跨越了2天,并且不确定在一个查询中是否可能出现这种情况

到目前为止,我已经得出以下结论:

select trunc(timestamp) as DTE, extract(hour from timestamp) as HR, count(COLUMN) as Total
from TABLE 
where trunc(timestamp) >= '12-NOV-19' and 
      extract(hour from timestamp) in ('23','00','01') 
group by trunc(timestamp), extract(hour from timestamp)
order by 1,2 desc; 
我希望的结果是这样的:

DTE         |  Total
20-NOV-19       5
19-NOV-19       4
18-NOV-19       4
17-NOV-19       6

在第一天,很多感谢< /P> < P>过滤器,将它与<代码> Tunc(SysDead)-间隔“7”天< /代码>进行比较,然后通过比较<代码>时间戳<代码>自身,将其截断到午夜,偏移数小时。

选择trunc(时间戳)作为DTE,
提取(从时间戳算起的小时数)为小时,
将(列)计为总数
从桌子上
其中时间戳>=TRUNC(SYSDATE)-间隔“7”天
和(时间戳=TRUNC(时间戳)+间隔“23:00”小时到分钟
)
按trunc分组(时间戳),提取(从时间戳开始的小时)
由DTE、HR desc订购;

减去或加上一小时即可得出日期。我不确定您希望为每个时段指定的日期,但我的想法是:

select trunc(timestamp - interval '1' hour) as DTE, 
       count(*) as Total
from t 
where trunc(timestamp - interval '1' hour) >= DATE '2019-11-12' and 
      extract(hour from timestamp) in (23, 0) 
group by trunc(timestamp - interval '1' hour)
order by 1 desc; 

注意:如果希望时间在晚上11:00到凌晨1:00之间,则希望时间为23或0。

示例表数据和匹配的预期结果使事情更容易理解。请看一看。示例数据将对您的问题大有帮助。您的查询有什么问题吗?
19年11月20日00:10:20
属于11月20日还是19日?