Insert Dates in the return from a query where there is none

前端 未结 5 1325
慢半拍i
慢半拍i 2020-12-01 22:09

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

5条回答
  •  隐瞒了意图╮
    2020-12-01 22:18

    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.

提交回复
热议问题