从 6 个表中获取中位数
Get the median from 6 tables
我知道如何从 1 table 计算中位数,但我试图在 6 table 秒内比较价格。到目前为止我的代码,你能帮忙吗?
select avg(price) as median from
(select row_id, price from (
(select @counter:=@counter+1 as row_id, t1.priceInt as price
from Table1 t1, (select @counter:=0) tx1
)
union all (select @counter:=@counter+1 as row_id, t2.priceInt as price
from Table2 t2, (select @counter:=0) tx2
)
union all (select @counter:=@counter+1 as row_id, t3.priceInt as price
from Table3 t3, (select @counter:=0) tx3
)
union all (select @counter:=@counter+1 as row_id, t4.priceInt as price
from Table4 t4, (select @counter:=0) tx4
)
union all (select @counter:=@counter+1 as row_id, t5.priceInt as price
from Table5 t5, (select @counter:=0) tx5
)
union all (select @counter:=@counter+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!
这里是Table1的例子,每个table都有相同的列!
***编辑
想要的结果:250, 275, 300, 400, 500 我想要 300k
(注意数字必须排序,如果有 2 个中间数字,则必须找到这 2 个数字的平均值)
就"stack"把每个table的数据放在一起,以后就当做一个列表,做计数器"up one level"。布局您的查询,使其易于阅读,以便容易找到别名。
select
avg(price) as median
from (
select
row_id
, price
from (
select
@counter:=@counter+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 作为 "list" 放置对我的经验很有帮助。我刚刚意识到我在保存之前把 table 的名字弄错了。它当然未经测试,但它应该可以帮助您开始。
哦,请避免在 table 之间或 from 子句中的子查询之间使用逗号。你会看到很多这样的样本:
from table_x, (select @counter:=0) vars
不要!它不是显式连接(它是隐含的交叉连接)使其显式:
from table_x
cross join (select @counter:=0) vars
现在每个人都知道交叉连接是有意的。
我知道如何从 1 table 计算中位数,但我试图在 6 table 秒内比较价格。到目前为止我的代码,你能帮忙吗?
select avg(price) as median from
(select row_id, price from (
(select @counter:=@counter+1 as row_id, t1.priceInt as price
from Table1 t1, (select @counter:=0) tx1
)
union all (select @counter:=@counter+1 as row_id, t2.priceInt as price
from Table2 t2, (select @counter:=0) tx2
)
union all (select @counter:=@counter+1 as row_id, t3.priceInt as price
from Table3 t3, (select @counter:=0) tx3
)
union all (select @counter:=@counter+1 as row_id, t4.priceInt as price
from Table4 t4, (select @counter:=0) tx4
)
union all (select @counter:=@counter+1 as row_id, t5.priceInt as price
from Table5 t5, (select @counter:=0) tx5
)
union all (select @counter:=@counter+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!
这里是Table1的例子,每个table都有相同的列!
***编辑
想要的结果:250, 275, 300, 400, 500 我想要 300k (注意数字必须排序,如果有 2 个中间数字,则必须找到这 2 个数字的平均值)
就"stack"把每个table的数据放在一起,以后就当做一个列表,做计数器"up one level"。布局您的查询,使其易于阅读,以便容易找到别名。
select
avg(price) as median
from (
select
row_id
, price
from (
select
@counter:=@counter+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 作为 "list" 放置对我的经验很有帮助。我刚刚意识到我在保存之前把 table 的名字弄错了。它当然未经测试,但它应该可以帮助您开始。
哦,请避免在 table 之间或 from 子句中的子查询之间使用逗号。你会看到很多这样的样本:
from table_x, (select @counter:=0) vars
不要!它不是显式连接(它是隐含的交叉连接)使其显式:
from table_x
cross join (select @counter:=0) vars
现在每个人都知道交叉连接是有意的。