I need to calculate the median of user session length (ts_last-ts_first) by days (ts_first) in MySQL. Trying something like this but it does not work.
Thanks!
SET @row_number:=0;
SET @medin_group:=’’;
SELECT @row_number:=CASE
WHEN @median_group= FROM_UNIXTIME(first_ts, '%t') THEN @row_number+1
ELSE 1
END AS count_by_time,
@median_group:= FROM_UNIXTIME(first_ts, '%t') AS median_group,
FROM_UNIXTIME(first_ts, '%t') AS by_time,
AVG(last_ts-first_ts) AS length,
(SELECT
COUNT (*)
FROM
User_sessions
WHERE
a.by_time=by_time) AS total_by_time
FROM
(SELECT FROM_UNIXTIME(first_ts, '%t') AS by_time, AVG(last_ts-first_ts) AS length
FROM
User_sessions
ORDERS BY by_time, length) AS