# Users Last Seen Since (Since N Days Ago)

**URL:** <https://meta.discourse.org/t/users-last-seen-since-since-n-days-ago/275130>\
**Category:** Data & reporting\
**Tags:** sql-query\
**Created:** [May 1, 2016, 2:00am UTC](https://meta.discourse.org/t/users-last-seen-since-since-n-days-ago/275130 "2016-05-01T02:00:00Z")\
**Posts on this page:** 1\
**Page:** 1

## AI-generated summary

[mcwumbly](/t/-/275130/1) shares a SQL query to report on users last seen within the past 30 days. The query uses a Common Table Expression (CTE) named `intervals` to generate a series of daily time windows from 30 days ago up to yesterday. It then performs a `right join` between the `USERS` table and these intervals, counting users whose `last_seen_at` timestamp falls within each specific day's window. The results are grouped by the time interval and ordered by the start time in descending order, effectively providing a daily count of active users over the last month.

<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 1, 2016, 2:00am UTC](https://meta.discourse.org/t/users-last-seen-since-since-n-days-ago/275130/1 "2016-05-01T02:00:00Z")

</div>

### Users Last Seen Since (Since N Days Ago)

```sql

with intervals as (
    select
        n as start_time, 
        CURRENT_TIMESTAMP as end_time
    from generate_series(CURRENT_TIMESTAMP - INTERVAL '30 days',
                         CURRENT_TIMESTAMP - INTERVAL '1 day',
                         INTERVAL '1 day') n
)
select 
  COUNT(1) as users_seen_since,
  CURRENT_TIMESTAMP - i.start_time as time_ago
FROM USERS u
right join intervals i
on u.last_seen_at >= i.start_time and u.last_seen_at < i.end_time
group by i.start_time, i.end_time
order by i.start_time desc

```
