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
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)
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? –
Das klingt wie ein Schema, das schlecht normalisiert ist. Warum sind Ihre Preise auf sechs Tische verteilt? – duffymo