我有30分钟的数据,想要从当前日期的02:00 AM到下一个日期的02:00 AM的白天和小组,我该怎么办?:
create table test (code varchar2(50), hour number, daytime date);
insert into test (code, hour, daytime) values ('K11','0.5','23-Feb-17')
insert into test (code, hour, daytime) values ('K11','0.5','23-02-17 00:30')
insert into test (code, hour, daytime) values ('K11','0.5','23-02-17 01:00')
insert into test (code, hour, daytime) values ('K11','0.5','23-02-17 01:30')
insert into test (code, hour, daytime) values ('K11','0.5','23-02-17 02:00')
insert into test (code, hour, daytime) values ('K11','0.5','23-02-17 02:30')
insert into test (code, hour, daytime) values ('K11','0.5','24-Feb-17')
insert into test (code, hour, daytime) values ('K11','0.5','24-02-17 00:30')
insert into test (code, hour, daytime) values ('K11','0.5','24-02-17 01:00')
insert into test (code, hour, daytime) values ('K11','0.5','24-02-17 01:30')
insert into test (code, hour, daytime) values ('K11','0.5','24-02-17 02:00')
insert into test (code, hour, daytime) values ('K11','0.5','24-02-17 02:30')
insert into test (code, hour, daytime) values ('K12','0.5','23-Feb-17')
insert into test (code, hour, daytime) values ('K12','0.5','23-02-17 00:30')
insert into test (code, hour, daytime) values ('K12','0.5','23-02-17 01:00')
insert into test (code, hour, daytime) values ('K12','0.5','23-02-17 01:30')
insert into test (code, hour, daytime) values ('K12','0.5','23-02-17 02:00')
insert into test (code, hour, daytime) values ('K12','0.5','23-02-17 02:30')
insert into test (code, hour, daytime) values ('K12','0.5','24-Feb-17')
insert into test (code, hour, daytime) values ('K12','0.5','24-02-17 00:30')
insert into test (code, hour, daytime) values ('K12','0.5','24-02-17 01:00')
insert into test (code, hour, daytime) values ('K12','0.5','24-02-17 01:30')
insert into test (code, hour, daytime) values ('K12','0.5','24-02-17 02:00')
insert into test (code, hour, daytime) values ('K12','0.5','24-02-17 02:30')
最后我想得到以下数据:
'K11',3,'23-02-17'
'K12',3,'23-02-17'
因此,例如K11从('K11','0.5','23-02-17 02:00')到('K11','0.5','24-02-17 01: 00')
答案 0 :(得分:0)
您可以通过减去两个小时来完成此操作。这是一种方法:
select trunc(date - 2/24) as dte, sum(hours)
from t
group by trunc(date - 2/24)
order by dte;
Oracle现在支持interval
语法,因此您也可以使用d:
select trunc(date - interval 2 hour) as dte, sum(hours)
from t
group by trunc(date - interval 2 hour)
order by dte;