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

Sure, give this a try:

--[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
username,
dbv.user_id AS id,
visited_at,
time_away
FROM days_between_visits dbv
JOIN users u ON u.id = dbv.user_id
WHERE time_away >= :days_away
ORDER BY visited_at DESC

Let me know if you get any timeout errors when running it. Note that I have still included the user’s ID in the results that are returned. That column is now labeled id. If you don’t want the ID to be returned, remove the dbv.user_id AS id, line from the final SELECT statement.