We are building a query to count the number of events per hour, per day. Most days there are hours that do not have any activity and therefore where the query is run the cou
The basic answer here involves a left outer join (LOJ), and an explicit COUNT(column) since that does not count nulls but COUNT(*) counts all rows. The hard part is generating a table against which to do the LOJ. The WITH clause and recursive solution will work in a number of DBMS (MS SQL Server, apparently, and almost certainly DB2 -- probably others too).
Many DBMS support temporary tables and stored procedures; the combination could be used to populate a table with an appropriate set of values for the date/time field, and then do the LOJ against that table (or, more precisely, FROM temp_table LEFT OUTER JOIN main_table ...). Not as neat and tidy, but works most places.