I'm trying to figure out where to put the following INNER JOIN
statement:
INNER JOIN resort_blocked_task ON d.task_id = resort_blocked_task.blocked_task_fbid
Into this MySQL statement:
SELECT sq.team_id, avg(sq.time_owned) as median_val FROM (
SELECT t1.row_number, t1.time_owned, t1.team_id FROM(
SELECT IF(@prev!=d.team_id, @rownum:=1, @rownum:=@rownum+1) as `row_number`, d.time_owned, @prev:=d.team_id AS team_id, d.task_id
FROM task_timer d, (SELECT @rownum:=0, @prev:=NULL) r
ORDER BY team_id, time_owned
) as t1 INNER JOIN
(
SELECT count(*) as total_rows, team_id
FROM task_timer d
GROUP BY team_id
) as t2
ON t1.team_id = t2.team_id
WHERE 1=1
AND t1.row_number>=t2.total_rows/2 and t1.row_number<=t2.total_rows/2+1
)sq
group by sq.team_id
No matter where I'm trying to put the INNER JOIN too many results are being filtered out causing the median to be incorrect.
Here is the SQL Fiddle of some example data.