如何输出团队评分?

How do I output rating of teams?

我有 2 个表: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       |
|---------------------|------------------|
|          ...        |         ...      | 
|---------------------|------------------|

先在tableperformance里面聚合,条件聚合计算出各队的总分,然后把tableteam加入到结果中进行排名具有 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_id