我如何输出球队的评分?

问题描述 投票:0回答:1

我有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       |
|---------------------|------------------|
|          ...        |         ...      | 
|---------------------|------------------|
sql oracle rating
1个回答
0
投票

首先在表performance内进行合计以计算有条件聚合的每个团队的总积分,然后将表team与结果结合并使用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
© www.soinside.com 2019 - 2024. All rights reserved.