Finding missing hours in a fact table.
What's the most efficient way to find missing data (timestamps) between two specified timestamps in a fact table? Ideally what I'd like to accomplish is the equivalent of what can be done in PostgreSQL by joining from a temporary table created using the generate_series function. e.g. WITH expected_hours AS ( SELECT s.tstamp AS expected_tstamp FROM GENERATE_SERIES ( '2013-08-07 00:00:00.000'::TIMESTAMP, '2013-08-07 23:59:59.999'::TIMESTAMP, '1 hour'::INTERVAL ) AS s(tstamp) ), fact_table AS ( SELECT s.tstamp AS tstamp FROM GENERATE_SERIES ( '2013-08-07 02:00:00.000'::TIMESTAMP, '2013-08-07 04:59:59.999'::TIMESTAMP, '1 hour'::INTERVAL ) AS s(tstamp) UNION ALL SELECT s.tstamp AS tstamp FROM GENERATE_SERIES ( '2013-08-07 06:00:00.000'::TIMESTAMP, '2013-08-07 21:59:59.999'::TIMESTAMP, '1 hour'::INTERVAL ) AS s(tstamp) ) SELECT expected_tstamp AS missing_tstamp FROM expected_hours LEFT OUTER JOIN fact_table ON (tstamp = expected_tstamp) WHERE tstamp IS NULL; missing_tstamp --------------------- 2013-08-07 00:00:00 2013-08-07 01:00:00 2013-08-07 05:00:00 2013-08-07 22:00:00 2013-08-07 23:00:00 (5 rows) I looked at TIME_SLICE and TIME_SERIES but couldn't figure out how to accomplish this. Thanks!
0
Comments