mysql - SQLusing abs中的中位数

标签 mysql sql statistics median

我能够使用下面的逻辑编写查询来查找中位数,但我在尝试理解逻辑时遇到了麻烦。有人可以帮我了解发生了什么。我从一本高级 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;

最佳答案

这按销售数字对笛卡尔乘积进行分组,以找到“中间”的 1 或 2 次销售,然后将结果取平均给定中位数。查看在线的详细评论。

--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;

关于mysql - SQLusing abs中的中位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26270803/

相关文章:

c++ - 如何从 linux C++ 应用程序访问 SQL 服务器?

python - 基于其他列中的数据的列表中单词的频率

PHP 驱动的详尽统计数据 - 服务器端文本文件或 MySQL 表?

mysql - 与 Mariadb 10.2 相比,Mysql 5.5 中的查询非常慢

php - PDO MYSQL 输出 false

mysql - NodeJS 服务器在使用 "Incorrect arguments"查询 MySQL 数据库时崩溃并抛出错误

python - 如何使用 statsmodels 执行 2-way 重复测量方差分析?

mysql - 如果条件不成立,如何选择无?

sql - 如何查找只有一个值的列 - Postgresql

C# ASP.Net Parameters.AddWithValue 拒绝参数的空值