Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Generate a single-column <code>dates_hours</code> table which contains all dates and hours within a reasonable range (e.g. from 1900 to 2200). Then do a <code>LEFT JOIN</code> from this table to your current query.</p> <p>For this technique to perform correctly, you will probably need to add an indexed column to your table which contains a converted time stamp (your <code>copied_timestamp</code> converted to <code>DATETIME</code>, rounded to the hour)</p> <pre class="lang-sql prettyprint-override"><code>SELECT date_hour, count(req.converted_timestamp) FROM dates_hours LEFT JOIN req ON req.converted_timestamp = dates_hours.date_hour WHERE date_hour BETWEEN (SELECT MIN(req.converted_timestamp) FROM req) AND (SELECT MAX(req.converted_timestamp) FROM req) GROUP BY date_hour </code></pre> <p>To generate the <code>dates_hours</code> table:</p> <pre class="lang-sql prettyprint-override"><code>CREATE TABLE dates_hours (date_hour DATETIME PRIMARY KEY); DELIMITER $$$ CREATE PROCEDURE generate_dates_hours (to_date DATETIME) BEGIN DECLARE start_date DATETIME; DECLARE inc INT; SELECT MAX(date_hour) INTO start_date FROM dates_hours; IF start_date IS NULL THEN SET start_date = '1900-01-01'; END IF; SET inc = 1; WHILE start_date + INTERVAL inc HOUR &lt;= to_date DO INSERT INTO dates_hours VALUE (start_date + INTERVAL inc HOUR); SET inc = inc +1; END WHILE; END $$$ DELIMITER ; CALL generate_dates_hours('2200-01-01'); </code></pre> <p>Okay, now that I am proof-reading myself, I realise this is quite far-fetched a solution. I hope someone comes up with a more elegant one.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload