Run a query based on a timeframe

Hi all, just writing a query to find out who hasn’t been seen in the last year on the forum so wanted to ask what the syntax would be. tried last_seen_at > 2022:06:21 and got an error

It will want a timestamp rather than a date, so something like this:

SELECT id AS user_id,
       last_seen_at::date
FROM users
WHERE last_seen_at < '2022-06-21T00:00:00Z'
ORDER BY last_seen_at DESC

Alternatively, you could CAST the date:


SELECT id AS user_id,
       last_seen_at::date
FROM users
WHERE last_seen_at < CAST('2022-06-21' AS date)
ORDER BY last_seen_at DESC

Or you could add a parameter to make it a bit more versatile (this one has a default):

-- [params]
-- date :date = 21/06/2022

SELECT id AS user_id,
       last_seen_at::date
FROM users
WHERE last_seen_at < :date
ORDER BY last_seen_at DESC

5 Likes

I got errors with the time stamp but might be because I missed the single quotes out

2 Likes