Show data for last X days

Hello
I’m trying to run a report how shown the visits during the last 30 days, so I used this query but apparently, it’s wrong :

select  distinct user_id, max(visited_at) as visited_at from user_visits
WHERE CAST(visited_at  AS DATE) >= GETDATE() -30
group by user_id

I tested also this condition

WHERE CAST(visite d_at AS DATE) >= CAST(GETDATE() -30 AS DATE)

is it possible to have this kind of conditions ?
Thank you

1 Like

Try

SELECT
  user_id,
  COUNT(*) AS visits
FROM user_visits
WHERE visited_at > CURRENT_DATE - 30
GROUP BY 1
ORDER BY 2 DESC
2 Likes

thank you @Falco , it works fine

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.