我有一些数据有开始和结束日期,我需要总结。我不知道如何为它编码。
以下是我必须使用的数据:
STARTTIME, STOPTIME, EVENTCAPACITY
8/12/2009 1:15:00 PM, 8/12/2009 1:59:59 PM, 100
8/12/2009 2:00:00 PM, 8/12/2009 2:29:59 PM, 100
8/12/2009 2:30:00 PM, 8/12/2009 2:59:59 PM, 80
8/12/2009 3:00:00 PM, 8/12/2009 3:59:59 PM, 85
在这个例子中,我需要从下午1点到下午2点,下午2点到下午3点以及下午3点到下午4点之间的总和
任何建议都表示赞赏。
史蒂夫
答案 0 :(得分:4)
如下:
SELECT TRUNC(stoptime,'HH'), sum(eventcapacity)
FROM yourtable
GROUP BY TRUNC(stoptime,'HH');
答案 1 :(得分:1)
你需要一个数字表:
select sum(e.capacity), n.value from eventtable e
left outer join numbers n on n.value between
extract(hours from e.starttime) and extract(hours from e.stoptime)
where n.value between 0 and 23
group by n.value
order by n.value
数字表有一个列(值),并填充0到100(或更多)的整数值,但在这种情况下,您只需要0到23。
create table number (
value number(4) not null,
primary key (value)
);
答案 2 :(得分:0)
我不确定PL / SQL的确切语法,但是这样的事情应该这样做(虽然它非常不合适):</ p>
select sum(capacity), case when to_char(starttime, 'HH') between '13' and '14'
and to_char(stoptime, 'HH') between '13' and '14'
then '1pm-2pm'
case when to_char(starttime, 'HH') between '14' and '15'
and to_char(stoptime, 'HH') between '14' and '15'
then '2pm-3pm'
(etc.)
as timeslot
from eventtable
group by timeslot