我有两张桌子:team(team_id, name),performance(team_id, stage, place, date)。各队应该获得积分:第一名-10分,第二名5,第三-3分,4-7位1分。我需要输出球队的等级。我觉得应该是:
SELECT team.name, CASE place
WHEN 1 points + 10
WHEN 2 points + 5
...预期理由:
|---------------------|------------------|
| team.name | Points |
|---------------------|------------------|
| Rockers | 34 |
|---------------------|------------------|
| Batmans | 23 |
|---------------------|------------------|
| ... | ... |
|---------------------|------------------|发布于 2020-05-25 15:35:42
首先在表performance中进行聚合,以计算每个团队的有条件聚集的总分,然后将表team加入到结果中,并使用RANK()或DENSE_RANK()分析函数对团队进行排序:
select t.team_id, t.name,
coalesce(p.points, 0) points,
rank() over (order by coalesce(p.points, 0) desc) rnk
from team t left join (
select team_id,
sum(
case
when place = 1 then 10
when place = 2 then 5
when place = 3 then 3
when place <= 7 then 1
else 0
end
) points
from performance
group by team_id
) p on p.team_id = t.team_idhttps://stackoverflow.com/questions/62005504
复制相似问题