SQL中位数问题

  1. 建立两个累计频率, 升序降序, 中位数等于 平均值 of(当升序累计频率>=总频率/2 且降序累计频率>=总频率/2 时的值)
select
avg(t.number) as median
from
(
select
n1.number,
n1.frequency,
#求累计频率, 建立自连接表
(select sum(frequency) from numbers n2 where n2.number<=n1.number) as asc_frequency,
(select sum(frequency) from numbers n3 where n3.number>=n1.number) as desc_frequency
from numbers n1
) t
where t.asc_frequency>= (select sum(frequency) from numbers)/2
and t.desc_frequency>= (select sum(frequency) from numbers)/2

你可能感兴趣的:(SQL中位数问题)