MySQL聚合连接?
MySQL aggregated join?
我有一个 table 架构:Table(排名,得分)。
我需要进行查询以获取排名高于每个排名的当前排名的分数的平均值。
查询(等级,average_score_of_greater_ranks)。
我试过类似的方法:
select `rank`, avg_score_of_greater_ranks
from Table t1
join (SELECT AVG(Revenue_Amount) as avg_score_of_greater_ranks
from Table t2
where t1.rank > t2.rank) as temp
order by `rank`
但是没用。任何帮助将不胜感激。
I need to make a query that gets the average of the scores that has a rank that is greater than the current rank for each rank. Query(rank, average_of_lower_ranks).
假设排名是基于分数,这只是一个累积平均值。对于大于或等于给定分数的分数的平均值:
select t1.*,
avg(`rank`) over (order by `rank` desc)
from Table t1;
如果你只想要那些严格更大的平均值,那就有点狡猾了。假设 rank
是一个整数,你可以使用:
select t1.*,
avg(`rank`) over (order by `rank`
range between 1 following and unbounded following
) as avg_rank_greater
from t1;
如果排名与分数无关,可以将上述表达式中的order by rank
替换为order by score
。
我有一个 table 架构:Table(排名,得分)。 我需要进行查询以获取排名高于每个排名的当前排名的分数的平均值。 查询(等级,average_score_of_greater_ranks)。
我试过类似的方法:
select `rank`, avg_score_of_greater_ranks
from Table t1
join (SELECT AVG(Revenue_Amount) as avg_score_of_greater_ranks
from Table t2
where t1.rank > t2.rank) as temp
order by `rank`
但是没用。任何帮助将不胜感激。
I need to make a query that gets the average of the scores that has a rank that is greater than the current rank for each rank. Query(rank, average_of_lower_ranks).
假设排名是基于分数,这只是一个累积平均值。对于大于或等于给定分数的分数的平均值:
select t1.*,
avg(`rank`) over (order by `rank` desc)
from Table t1;
如果你只想要那些严格更大的平均值,那就有点狡猾了。假设 rank
是一个整数,你可以使用:
select t1.*,
avg(`rank`) over (order by `rank`
range between 1 following and unbounded following
) as avg_rank_greater
from t1;
如果排名与分数无关,可以将上述表达式中的order by rank
替换为order by score
。