2014-10-09 2 views
2

Я смог написать запрос, чтобы найти медиану, используя следующую логику ниже, где у меня возникают проблемы, пытаясь понять логику. Может кто-то, пожалуйста, помогите мне понять, что происходит. Я получил код из предварительной книги sql.медиана в SQLusing 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; 

ответ

1

Это группа декартово произведение по количеству продаж, чтобы найти «среднюю» 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; 
Смежные вопросы