Skip to content

Instantly share code, notes, and snippets.

@terrafied
Created October 26, 2012 17:14
Show Gist options
  • Save terrafied/3960022 to your computer and use it in GitHub Desktop.
Save terrafied/3960022 to your computer and use it in GitHub Desktop.
Timeseries query in Postgres
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