sql - MySQL query to determine classes remaining or completed -
i have following data in webinar_timing table in mysql database
start_time , end_time of type datetime
id | webinar_id | start_time | end_time ------------------------------------------------------------------- 1 | 5 | 3/18/2015 6:00:00 pm | 3/18/2015 7:00:00 pm 2 | 5 | 3/19/2015 6:00:00 pm | 3/19/2015 7:00:00 pm 3 | 5 | 3/20/2015 6:00:00 pm | 3/20/2015 7:00:00 pm 4 | 5 | 3/21/2015 6:00:00 pm | 3/21/2015 7:00:00 pm 5 | 5 | 3/22/2015 6:00:00 pm | 3/22/2015 7:00:00 pm 6 | 11 | 3/20/2015 8:00:00 pm | 3/20/2015 9:00:00 pm 7 | 11 | 3/21/2015 8:00:00 pm | 3/21/2015 9:00:00 pm 8 | 11 | 3/22/2015 8:00:00 pm | 3/22/2015 9:00:00 pm 9 | 22 | 3/25/2015 8:00:00 pm | 3/25/2015 9:00:00 pm 10 | 22 | 3/27/2015 8:00:00 pm | 3/27/2015 9:00:00 pm 11 | 22 | 3/29/2015 8:00:00 pm | 3/27/2015 9:00:00 pm
basically, each webinar, want total occurences , number of classes completed or remaining , next upcoming class
egs: when run query @ 3/21/2015 @ 4:00 pm - result expecting
webinar_id | total | classes completed | next class ---------------------------------------------------------- 5 | 5 | 3 | 3/21/2015 6:00:00 pm 11 | 3 | 1 | 3/21/2015 8:00:00 pm 22 | 3 | 0 | 3/25/2015 8:00:00 pm
or
webinar_id | total | classes remaining | next class ---------------------------------------------------------- 5 | 5 | 2 | 3/21/2015 6:00:00 pm 11 | 3 | 2 | 3/21/2015 8:00:00 pm 22 | 3 | 3 | 3/25/2015 8:00:00 pm
any appreciated in advance
just use sum(if(...))
select webinar_id, count(*) total, sum(if(end_time<now(), 1, 0)) completed, sum(if(start_time>=now(), 1, 0)) remaining webinar_times group webinar_id;
you'll have figure out whether looking @ start_time or end_time , equals , little details that...
Comments
Post a Comment