【问题标题】:median in SQLusing absSQL中的中位数使用abs
【发布时间】:2014-12-03 22:25:03
【问题描述】:
我能够使用以下逻辑编写查询以查找中位数,但我在尝试理解逻辑时遇到了麻烦。有人可以帮我理解发生了什么吗。我从一本高级 sql 书中得到了代码。
此代码特别适用于奇数和偶数。我尝试了代码并且它可以工作,但是我很好奇理解其中的逻辑。
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;
【问题讨论】:
标签:
mysql
sql
statistics
median
【解决方案1】:
这会按销售额对笛卡尔产品进行分组,以找到“中间”的 1 或 2 次销售额,然后将结果平均到给定的中间值。在线查看详细的 cmets。
--the subquery will return the 1 or 2 middle values and the average of those is the median
select avg(sales * 1.0) as median
from (
select g1.sales
--this creates a cartesian product of ga with itself
from ga g1, ga g2
--and then group by sales, which allows comparision of each given sales figure all others
group by g1.sales
having
--the sum(case) here acts a count of row in the cartesian product that have matching sales values
--this will be the the square of the count() from ga where for each given sales number
sum(
case
when g1.sales = g2.sales
then 1
ELSE 0
END)
>= --The comparison acts as a de-weighting mechanism to handle duplicate sales numbers
--Such that if I have the same sales figure twice I'll have 4 rows in the Cartesian product
--and I should see a matching 4 or 0 if the row can be used in the final median calculation
--the abs(sum(sign())) here acts as a measure of how far of the median each sales is
--by looking at how many sales are greater then, equal, a lesser. The sales at or nearest
--the median will have the lowest numbers here.
ABS(
SUM(
SIGN(g1.sales-g2.sales)
)
)
)g3;