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
We had a similar problem with some performance monitoring software but, being in a DB2/z mainframe shop, we're dead set against having to do SQL gymnastics to get those sort of results. SQL queries that perform 'functions' on every row they retrieve are notoriously unscalable and the DBAs would have a field day laughing at us if we tried to use them.
Instead, we found it easier to refactor the database schema to include a count of events in each row (apparently our DBAs don't mind using more disk space, just more CPU grunt). In your case, that would be adding a column called tdm_quant which you would set to 1 for every row that you insert (i.e., each event).
Then the fifth field of your query changes from count(tdm_msg) to sum(tdm_quant) which will achieve the same result.
In addition to that you can insert a special record (once an hour, or 24 of them at the start of each day, or populate the entire years worth on January 1 if you wish) where the tdm_quant field is set to zero. Being zero, these records will have no effect on the sum(tdm_quant) but you will get your desired behaviour, a row returned for every hour of the day which will have zero as Total_ACTIVITIES where no events occurred in that hour.
The rest of your query will not need to change.