# Daily, weekly, or total stats by user over a specified time range

**URL:** <https://meta.discourse.org/t/daily-weekly-or-total-stats-by-user-over-a-specified-time-range/275167>\
**Category:** Data & reporting\
**Tags:** sql-query\
**Created:** [December 31, 2019, 8:08pm UTC](https://meta.discourse.org/t/daily-weekly-or-total-stats-by-user-over-a-specified-time-range/275167 "2019-12-31T20:08:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grayden\_Shand](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/grayden_shand/32/166586_2.png) [@Grayden\_Shand](https://meta.discourse.org/u/Grayden_Shand)\
**Post date:** [December 31, 2019, 8:08pm UTC](https://meta.discourse.org/t/daily-weekly-or-total-stats-by-user-over-a-specified-time-range/275167/1 "2019-12-31T20:08:57Z")

</div>

```
-- coverage: 'week', 'all', or 'date'
-- [params]
-- date :start_date = 2019-08-27
-- date :end_date = 2019-09-30
-- text :coverage = week

WITH date_range AS (
SELECT date_trunc('day', dd):: date AS "date", EXTRACT(week from date_trunc('day', dd):: date) AS "week"
FROM generate_series
        ( :start_date::timestamp 
        , :end_date::timestamp
        , '1 day'::interval) dd
), likes_given AS (
SELECT u.id, dr.date, dr.week, count(pa.*) AS "likes"
FROM date_range dr
FULL JOIN users u ON (1=1)
LEFT JOIN post_actions pa ON (pa.created_at:: date = dr.date and post_action_type_id=2 AND user_id = u.id)
GROUP BY dr.date, dr.week, u.id
ORDER BY u.id, dr.date
), posts_summary AS (SELECT u.id, u.username, u.created_at, dr.*, count(p.id) - count(t.id) AS replies, count(t.id) AS topics, COALESCE(sum(p.like_count),0) AS likes_received
from date_range dr
FULL OUTER JOIN users u ON (1=1)
LEFT JOIN posts p ON (p.user_id = u.id AND p.created_at::date=dr.date AND p.deleted_at IS NULL)
LEFT JOIN topics t ON (t.user_id = u.id AND t.created_at::date = dr.date AND p.topic_id = t.id AND t.deleted_at IS NULL)
GROUP BY u.id, dr.date, dr.week
ORDER BY u.id, dr.date), 
visits AS (SELECT u.id, dr.*, COALESCE(sum(posts_read),0) AS posts_read, COALESCE(sum(time_read),0) AS time_read, COUNT(uv.*) AS visits
FROM date_range dr
FULL OUTER JOIN users u ON (1=1)
LEFT JOIN user_visits uv ON (uv.user_id = u.id AND visited_at = dr.date)
GROUP BY u.id, dr.date, dr.week
ORDER BY u.id, dr.date
)

SELECT ps.id, ps.username, ps.created_at, CASE
WHEN :coverage::text = 'week' THEN ps.week::text
WHEN :coverage::text = 'all' THEN '-1'
ELSE ps.date::text
END AS period, sum(ps.replies) AS replies, sum(ps.topics) AS topics, sum(ps.likes_received) AS likes_received, sum(lg.likes) AS likes_given, COALESCE(sum(posts_read),0) AS posts_read, COALESCE(sum(time_read),0) AS time_read, SUM(visits) AS visits
FROM posts_summary ps
LEFT JOIN likes_given lg ON (ps.date = lg.date AND ps.id = lg.id)
LEFT JOIN visits v ON (v.id = ps.id AND v.date = ps.date)
GROUP BY ps.id, ps.username, ps.created_at, CASE 
WHEN :coverage::text = 'week' THEN ps.week::text
WHEN :coverage::text = 'all' THEN '-1'
ELSE ps.date::text
END  
ORDER BY ps.id, CASE
WHEN :coverage::text = 'week' THEN ps.week::text
WHEN :coverage::text = 'all' THEN '-1'
ELSE ps.date::text
END

```

You can use this one to pull daily, weekly, or total stats by user over a specified time range.  
**Input params:**

- start\_date (string) e.g. ‘2019-12-31’
- end\_date (string) e.g. ‘2019-12-31’
- coverage (enum) one of 1) ‘date’ for daily 2) ‘week’ for weekly or 3) ‘all’ for total

**Output Fields:**

- id (user\_id)
- username
- created\_at (user created at)
- period (date string for ‘date’ coverage, week number (e.g. dec 26-31 = 52 for ‘week’ coverage, or -1 for ‘all’ coverage)
- replies (number of posts that are not the first post in a topic)
- topics
- likes\_received
- likes\_given
- posts\_read
- time\_read
- visits

You’ll likely need to use the API to access the full results of this query (especially for daily data over a long period of time). See [this post](https://meta.discourse.org/t/how-to-run-data-explorer-queries-with-the-discourse-api/120063) for more details on how to trigger queries via the API.

You can use this query to generate a data set of daily user engagement.
