468x60

venerdì 11 marzo 2016

Calculating Median with MySQL

Query per calcolare la Mediana in MySQL
select avg(test_val) as median from
(
select @counter:=@counter+1 as row_id, t1.test_val 
from test100 t1, (select @counter:=0) t2
order by t1.test_val
) o1
join
(
select count(*) as total_rows
from test100
) o2
where o1.row_id in (floor((o2.total_rows + 1)/2), floor((o2.total_rows + 2)/2))

Nessun commento:

Posta un commento

468x60

Cerca su Google

Cerca nel Blog con Google