sql - GROUP BY with date range -
i have table 4 columns, id
, stream
text, duration
(int), , timestamp
(datetime). there row inserted every time plays specific audio stream on website. stream
name, , duration
time in seconds listening. using following query figure total listen hours each week in year:
select yearweek(`timestamp`), (sum(`duration`)/60/60) logs_main `stream`="asdf" group yearweek(`timestamp`);
this expect... presenting total of listen time each week in year there data.
however, build query have result row weeks there may not data. example, if 26th week of 2006 has no rows fall within week, sum
result 0.
is possible this? maybe via join on date range somehow?
the tried true old school solution set table bunch of date ranges can outer join grouping (as in other table have of weeks in begin / end date).
in case, table full of values yearweek:
201100 201101 201102 201103 201104
and here sketch of sql statement:
select year_weeks.yearweek , (sum(`duration`)/60/60) year_weeks left outer join logs_main on year_weeks.yearweek = logs_main.yearweek(`timestamp`) `stream`="asdf" group year_weeks.yearweek;
Comments
Post a Comment