```
select sum(num)/2 median
from
(
select min(number) num from
(
select number, cumSum
from
(
SELECT n1.number, sum(case when n2.number < n1.number then n2.frequency when n2.number = n1.number then n1.frequency else 0 end) cumSum
FROM ngram.Numbers n1 join numbers n2
group by n1.number
order by n1.number
) cum
join
(select sum(frequency),
case
when sum(frequency) % 2 = 0 then sum(frequency) div 2
else (sum(frequency) div 2) + 1
end as idx1
from numbers
) idxs
where cumSum >= idx1
) firstNum
union all
select min(number) num from
(
select number, cumSum
from
(
SELECT n1.number, sum(case when n2.number < n1.number then n2.frequency when n2.number = n1.number then n1.frequency else 0 end) cumSum
FROM ngram.Numbers n1 join numbers n2
group by n1.number
order by n1.number
) cum
join
(select sum(frequency),
case
when sum(frequency) % 2 = 0 then (sum(frequency) div 2) + 1
else (sum(frequency) div 2) + 1
end as idx2
from numbers
) idxs
where cumSum >= idx2
) secondNum
)allNums
```