r/learnSQL • u/MikeyMicky • May 13 '26
Help with a CTE question
I'm trying to solve this question and my solution is off by 0.5seconds, can someone please tell me why, here's my code:
https://platform.stratascratch.com/coding/10352-users-by-avg-session-time?code_type=5
-- 1. define one session = one user per day
-- 2. session_time = max page_load and min page_exit is one session duration
-- 3. output user_id, AVG(session_time)
WITH SESSION_START as
(
SELECT
user_id,
session,
MAX(timestamp) as session_start
FROM(
SELECT
*,
DENSE_RANK() OVER (PARTITION BY user_id ORDER BY CAST(timestamp AS DATE)) AS session
FROM facebook_web_log
WHERE action = 'page_load')t
GROUP BY
user_id,
session
),
SESSION_END AS
(
SELECT
user_id,
session,
MIN(timestamp) as session_end
FROM(
SELECT
*,
DENSE_RANK() OVER (PARTITION BY user_id ORDER BY CAST(timestamp AS DATE)) AS session
FROM facebook_web_log
WHERE action = 'page_exit')t
GROUP BY
user_id,
session
)
SELECT
s.user_id,
AVG(DATEDIFF(SECOND,s.session_start, e.session_end)) as AVG_SESSION_DURATION
FROM SESSION_START s
JOIN SESSION_END e
ON s.user_id = e.user_id
AND s.session = e.session
GROUP BY s.user_id
View all comments
Show parent comment
2
u/SQLDevDBA May 13 '26
Glad you got it solved!! Yes I always get tripped up with that when it comes to AVG and also division or multiplication. It will always implicitly convert to an INT if it can, so just watch out for those. Nice work!