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/MikeyMicky May 13 '26 •
You're right! I had to cast the result of the DATEDIFF as a decimal before averaging it! I made this update to the main query: AVG(CAST(DATEDIFF(SECOND,s.session_start, e.session_end) AS DECIMAL (10,2))) as AVG_SESSION_DURATION
What a small detail I overlooked, and what a wonderful feeling to have found the answer, thank you so much!