-1

In the below query:

SELECT user_id, (max(ts) -  min(ts)) as tsdiff
               FROM analytics 
               GROUP BY user_id

How can I filter on on tsdiff which I assume is a interval? For example I'd like to filter for all records where tsdiff is larger than 1d (one day).

Even if I were to use a subquery, I'd face the same issue again.

3

Use a HAVING clause:

SELECT 
  user_id
 ,(max(ts) -  min(ts)) as tsdiff
FROM 
  analytics 
GROUP BY 
  user_id
HAVING
  max(ts) -  min(ts) > interval '1 day'

See: https://www.postgresql.org/docs/current/tutorial-agg.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.