Skip to content

Instantly share code, notes, and snippets.

@jeff
Last active July 14, 2019 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeff/4533909 to your computer and use it in GitHub Desktop.
Save jeff/4533909 to your computer and use it in GitHub Desktop.
Looking at grouping circ counts by "week" with some consideration given to avoiding partial weeks at the start of the data summarized.
-- This will show counts grouped by weeks, but will
-- likely include a partial week at the start
-- (check the "days_in_sample" column)
SELECT date_trunc('week',xact_start)::date week,
circ_lib,
count(distinct xact_start::date) days_in_sample,
count(*) circ_count
FROM action.circulation
WHERE xact_start > '2012-12-01'
GROUP BY 1,2
ORDER BY 1,2;
-- This will ensure that we have a
-- full week at the start of the data
-- (assuming that is possible)
-- again, see the "days_in_sample" column
SELECT date_trunc('week',xact_start)::date week,
circ_lib,
count(distinct xact_start::date) days_in_sample,
count(*) circ_count
FROM action.circulation
WHERE xact_start > '2012-12-01'
GROUP BY 1,2
HAVING date_trunc('week',xact_start)::date > '2012-12-01'
ORDER BY 1,2;
-- The example just before this excludes partial
-- weeks, at the expense of excluding data within
-- the range we requested.
-- This example favors pulling earlier than the
-- specified date in order to ensure a full week
SELECT date_trunc('week',xact_start)::date week,
circ_lib,
count(distinct xact_start::date) days_in_sample,
count(*) circ_count
FROM action.circulation
WHERE xact_start::date >= date_trunc('week', '2012-12-01'::date)::date
GROUP BY 1,2
ORDER BY 1,2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment