datetime - MySQL display statistics grouped by time slot -
i have 1 table called reservation such fields:
`id` int(11) not null auto_increment, `username` varchar(150) not null, `begin_time` datetime not null, `end_time` datetime not null, `node_id` int(11) not null, primary key (`id`)
i wish show statistics reservations during given time period (say $from - $until, values coming php). user books 1 node each reservation. there many nodes can booked , use following query show reservation statistics date.
select date(begin_time) `date`, count(id) reservations reservation begin_time > '" . $from . "' , end_time < '" . $until . "' group date"
now want give statistics on time period during day more active. 24 hours of day divided in 48 thirty-minute slots. in table, begin_date , end_date values round numbers 2011-04-11 20:00:00 or 2011-04-11 14:30:00. reservations can have duration of 30 minutes, 60 minutes, etc until 4 hours maximum.
i statistics in following format (more or less)
time slot | reservations ---------------------------------- 00:00:00 - 00:30:00 | 23 00:30:00 - 01:00:00 | 12 01:00:00 - 01:30:00 | 20 01:30:00 - 02:00:00 | 66 02:00:00 - 02:30:00 | 12 , on
can me sql query?
edit: see postgresql has range types , operators, example <@ (range contained by) documentation here. mean query easier if used postgres? mysql have similar?
edit 2: thank you, tombom. actually, changed answer, removing date(begin_time) date
select clause , group clause, because wanted see hours of day active over period of many days. , can live having 1 hour granularity instead of 30 mins. ask mean first sentence. if reservation starts @ 01:00:00 , ends @ 03:00:00 in fact want counted on both time slots.
in table, begin_date , end_date values round numbers 2011-04-11 20:00:00 or 2011-04-11 14:30:00
unless have times 2011-04-11 14:15:00
desired result steps of 30 minutes doesn't make sense, unless want count reservations 2 times.
apart in mysql this:
select date(begin_time) `date`, case when time(begin_time) >= '00:00:00' , time(begin_time) < '01:00:00' '00:00:00 - 01:00:00' when time(begin_time) >= '01:00:00' , time(begin_time) < '02:00:00' '01:00:00 - 02:00:00' /*...and on...*/ end `timeslot`, count(id) reservations reservation begin_time > '" . $from . "' , end_time < '" . $until . "' group `date`, `timeslot`
i made time slots of 1 hour , added date grouped on.
actually, silly me, think it, when have time slots of 1 hour, can this:
select date(begin_time) `date`, hour(begin_time) `timeslot`, count(id) reservations reservation begin_time > '" . $from . "' , end_time < '" . $until . "' group `date`, `timeslot`
update:
select case when time(begin_time) between '00:00:00' , '01:00:00' or time(end_time) between '00:00:00' , '01:00:00' '00:00:00 - 01:00:00' when time(begin_time) between '01:00:00' , '02:00:00' or time(end_time) between '01:00:00' , '02:00:00' '01:00:00 - 02:00:00' /*...and on...*/ end `timeslot`, count(id) reservations reservation begin_time > '" . $from . "' , end_time < '" . $until . "' group `timeslot`
Comments
Post a Comment