Created
October 26, 2012 17:14
-
-
Save terrafied/3960022 to your computer and use it in GitHub Desktop.
Timeseries query in Postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE VIEW opendiscrepencydailycounts AS | |
WITH opendays(day) AS ( | |
SELECT gs.day::date AS day | |
FROM generate_series((( SELECT | |
min(discrepencylist.discstartdt) AS min | |
FROM discrepencylist))::timestamp without time | |
zone, 'now'::text::date::timestamp without time zone, '1 | |
day'::interval) gs(day) | |
) | |
SELECT opendays.day, ds.resolvingparty, count(opendays.day) AS | |
opendiscrepancies | |
FROM discrepencylist ds, opendays | |
WHERE opendays.day >= ds.discstartdt AND opendays.day <= | |
LEAST('now'::text::date, ds.resolutiondate) | |
GROUP BY opendays.day, ds.resolvingparty | |
ORDER BY opendays.day, ds.resolvingparty; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment