2
我能夠使用下面的邏輯來編寫查詢來查找中位數,在這裏我遇到了麻煩是試圖理解邏輯。有人可以幫我理解發生了什麼事。我從一本高級的SQL書中得到了代碼。SQL中使用abs的中位數
具體來說,這個代碼適用於奇數和偶數。我嘗試了代碼,它的工作原理,但我很好奇理解邏輯。
select avg(sales) as median
from
(select g1.sales
from ga g1, ga g2
group by g1.sales
having sum(case when g1.sales = g2.sales then 1 ELSE 0 END) >= ABS(SUM(SIGN(g1.sales-g2.sales))))g3;