我需要在 MySQL 中按天数 (ts_first) 计算用户会话长度 (ts_last-ts_first) 的中位数。尝试这样的事情,但它不起作用。
谢谢!
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