# Recently Read Topics by User

**URL:** https://meta.discourse.org/t/recently-read-topics-by-user/275000
**Category:** Data & reporting
**Tags:** sql-query
**Created:** [May 4, 2016, 5:25am UTC](https://meta.discourse.org/t/recently-read-topics-by-user/275000 "2016-05-04T05:25:02Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![mcwumbly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mcwumbly/32/103861_2.png) [@mcwumbly](https://meta.discourse.org/u/mcwumbly)
#### Post date: [May 4, 2016, 5:25am UTC](https://meta.discourse.org/t/recently-read-topics-by-user/275000/1 "2016-05-04T05:25:02Z")

</div>

### Recently Read Topics by User

Show the topics with that have been opened by a given user in the past N days, sorted by the amount of time the user has spent in that topic. ([requested on feverbee](https://experts.feverbee.com/t/measuring-reading-engagement-with-the-discourse-data-explorer-plugin/2598/7?u=mcwumbly))

```plaintext
-- [params]
-- integer :user = 1
-- integer :since_days_ago = 7

with topic_timing as (
  select user_id, topic_id, sum(msecs) / 1000 as seconds
  from post_timings
  where user_id = :user
  group by user_id, topic_id
)
SELECT tv.topic_id,
    tv.user_id,
    tv.viewed_at,
    tt.seconds
from topic_views tv
left join topic_timing tt
on tv.topic_id = tt.topic_id
and tv.user_id = tt.user_id
where tv.user_id = :user
and viewed_at + :since_days_ago > CURRENT_TIMESTAMP
order by seconds desc

```
