I'm running Postgres 9.6. I have a user table:
id | integer | | not null | nextval('user_id_seq'::regclass)
name | character varying | | not null |
And I have a history
table:
actioned_by | integer | | |
category | character varying | | not null |
t_actioned | timestamp without time zone | | not null |
I want a way to annotate each user with an integer field num_days
that indicates for how many distinct 24 hour periods they have actions.
I know how to get the count of distinct calendar days on which each user was active:
SELECT d.actioned_by, COUNT(*) AS cnt FROM
(SELECT date_trunc('day', t_actioned) AS day, actioned_by
FROM history
GROUP BY day, actioned_by) d
GROUP BY actioned_by
ORDER BY cnt DESC;
But is there a way to measure these periods would be measured from the time of their first action, to allow for people who log in at five minutes to midnight and leave 10 minutes later?
I'll just be running this occasionally for analytics purposes, so it doesn't really matter if it's slow.