Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Created February 24, 2014 12:27
Show Gist options
  • Save chanmix51/9187504 to your computer and use it in GitHub Desktop.
Save chanmix51/9187504 to your computer and use it in GitHub Desktop.
statistic over time ranges
WITH
timerange (ts) AS (SELECT generate_series(date_trunc('hour', min(collected_at)), date_trunc('hour', max(collected_at)) + '1 day'::interval, '1 hour'::interval) FROM statistic)
SELECT
ts,
count(stat),
stddev(stat),
avg(stat)
FROM
timerange t
LEFT JOIN statistic s ON s.collected_at <@ tsrange(t.ts, t.ts + '1 hour'::interval, '[)')
GROUP BY 1
ORDER BY 1 ASC
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment