2017-11-13 7 views
0

Ich weiß, wie man den Median von 1 Tabelle berechnet, aber ich versuche, es auf 6 Tabellen zu tun, die den Preis vergleichen. Mein Code bis jetzt, können Sie helfen?Erhalten Sie den Median von 6 Tabellen

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

Mein Fehler ist, dass o1.row_id nicht erkannt wird!

Hier ist ein Beispiel für Tabelle 1, jede Tabelle hat identische Spalten!

*** EDIT

enter image description here

Wunschergebnisse: 250, 275, 300, 400, 500 Ich möchte die 300k (Beachten Sie die Nummern bestellt werden müssen und wenn es zwei mittleren Zahlen sind die Durchschnitt muss von den 2 Zahlen gefunden werden)

+0

Ich könnte schwören, dass ich eine Frage fast identisch mit dieser in den letzten 24 Stunden geschrieben gesehen habe. Hast du das schon mal gefragt? –

+0

Das klingt wie ein Schema, das schlecht normalisiert ist. Warum sind Ihre Preise auf sechs Tische verteilt? – duffymo

Antwort

1

"Stapeln Sie einfach die Daten von jeder der Tabellen zusammen und behandeln Sie das als eine Liste ab dann, tun Sie den Zähler" herauf eine Ebene ". Legen Sie Ihre Abfrage so an, dass sie leicht zu lesen ist und Aliase leicht zu finden sind.

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 

Platzieren repetitive sql als eine "Liste" ist sehr hilfreich in meiner Erfahrung. Ich habe gerade bemerkt, dass ich die Tabellennamen falsch hatte, kurz bevor ich das speicherte. Es ist natürlich nicht getestet, aber es sollte Ihnen helfen, loszulegen.

Oh, und bitte vermeiden Sie Kommas zwischen Tabellen oder Unterabfragen in der Klausel von. Sie werden viele Beispiele sehen:

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

Nicht! Es ist keine explizite Join (es ist eine implizite Cross-Join) machen es deutlich:

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

Jetzt weiß jeder, dass Kreuz exsts beitreten und ist gewollt.

+0

Großartige Arbeit, du bist ein Star – KDJ

Verwandte Themen