מצא משתמשים שחזרו לאחר תקופה

The details you need are in the user_visits table. The tricky part for me is subtracting the value of the previous visit from each visit entry. It seems to require a window function. Based on the reply to this question on StackExchange, it looks like the LEAD function will work for this.

Give this query a try and see if it’s returning the data you are looking for. It defaults to returning all user visits that occurred after a period of 180 days between visits. That value can be adjusted by setting the query’s days_away parameter.

--[params]
-- integer :days_away = 180
WITH days_between_visits AS (
SELECT
user_id,
visited_at,
LEAD(visited_at) OVER (PARTITION BY user_id ORDER BY visited_at DESC) AS previous_visit,
visited_at - LEAD(visited_at) OVER (PARTITION BY user_id ORDER BY visited_at DESC) AS time_away
FROM user_visits
)

SELECT * FROM days_between_visits WHERE time_away >= :days_away
ORDER BY visited_at DESC

Let me know if you are noticing any issues with the data that is returned.