2017-11-13 23 views
0

1 테이블에서 중앙값을 계산하는 방법을 알고 있지만 6 테이블에서 가격을 비교하려고합니다. 지금까지 내 코드를 도울 수 있습니까?6 개 테이블의 중앙값 얻기

select avg(price) as median from 
    (select row_id, price from (
    (select @counter:[email protected]+1 as row_id, t1.priceInt as price 
    from Table1 t1, (select @counter:=0) tx1 
    ) 
    union all (select @counter:[email protected]+1 as row_id, t2.priceInt as price 
    from Table2 t2, (select @counter:=0) tx2 
    ) 
    union all (select @counter:[email protected]+1 as row_id, t3.priceInt as price 
    from Table3 t3, (select @counter:=0) tx3 
    ) 
    union all (select @counter:[email protected]+1 as row_id, t4.priceInt as price 
    from Table4 t4, (select @counter:=0) tx4 
    ) 
    union all (select @counter:[email protected]+1 as row_id, t5.priceInt as price 
    from Table5 t5, (select @counter:=0) tx5 
    ) 
    union all (select @counter:[email protected]+1 as row_id, t6.priceInt as price 
    from Table6 t6, (select @counter:=0) tx6 
    ) 
    ) xx order by price) o1 join 
    (
    select sum(x) AS total_rows from 
    (
    select count(*) x from Table1 union all select count(*) x from 
    Table2 
    union all select count(*) x from Table3 union all select count(*) x 
    from Table4 
    union all select count(*) x from Table5 union all (select count(*) x 
    from Table6 
    ) 
    ) o2 where o1.row_id in (floor((o2.total_rows + 1)/2), 
    floor((o2.total_rows + 2)/2))) 

o1.row_id가 인식되지 않습니다.

다음은 표 1의 예입니다. 각 표에는 동일한 열이 있습니다!

*** 편집

enter image description here

원하는 결과 : 250, 275, 300, 400, 500은 내가 원하는 300K (2 개 중간 번호가있는 경우 숫자가 주문해야하고 주 평균은 2 개의 숫자로 이루어져야합니다.)

+0

나는 지난 24 시간 동안 게시 된 질문과 거의 동일한 질문을 본 적이 있음을 맹세 할 수 있습니다. 전에 물어 본 적이 있니? –

+0

이것은 정상적으로 처리되지 않은 스키마처럼 들립니다. 왜 당신의 가격은 6 개의 테이블에 흩어져 있습니까? – duffymo

답변

1

각 테이블의 데이터를 함께 "쌓아서"하나의리스트로 취급하십시오. 카운터를 "한 단계 위로"수행하십시오. 읽기 쉽도록 별칭을 쉽게 찾을 수 있도록 쿼리를 레이아웃하십시오.

select 
    avg(price) as median 
from (
     select 
       row_id 
      , price 
     from (  
      select 
        @counter:[email protected]+1 as row_id 
       , price  
      from (
       select t1.priceInt as price from Table1 t1 union all 
       select t2.priceInt as price from Table2 t2 union all 
       select t3.priceInt as price from Table3 t3 union all 
       select t4.priceInt as price from Table4 t4 union all 
       select t5.priceInt as price from Table5 t5 union all 
       select t6.priceInt as price from Table6 t6 
       ) u 
      cross join (select @counter:=0) vars 
      ORDER BY u.price 
      ) o1 
     cross join (
        select sum(x) AS total_rows 
        from (
         select count(*) x from Table1 union all 
         select count(*) x from Table2 union all 
         select count(*) x from Table3 union all 
         select count(*) x from Table4 union all 
         select count(*) x from Table5 union all 
         select count(*) x from Table6 
         ) c 
       ) o2 
     where o1.row_id in (floor((o2.total_rows + 1)/2),floor((o2.total_rows + 2)/2))) 
    ) d 

"목록"으로 반복되는 SQL을 배치하는 것은 매우 유용합니다. 방금 전에 테이블 이름이 잘못되었다는 것을 깨달았습니다. 물론 테스트되지는 않았지만 그것이 도움이 될 것입니다.

아, from 절에 테이블 또는 하위 쿼리 사이에 쉼표를 사용하지 마십시오. 다음과 같은 샘플이 많이 표시됩니다.

from table_x, (select @counter:=0) vars 

Do not! 그것은하지 않습니다 명시 적으로 그것을 명시 적으로 만들 (이 가입 묵시적 크로스) 가입 :

from table_x 
cross join (select @counter:=0) vars 

지금 모두가 그 십자가 exsts에 가입하고 의도적 알고있다.

+0

당신은 스타입니다. – KDJ