我有3张桌子:
PLAYER (NAME)
ALL_GOALS (TYPE, GROUP , POINTS)
GOALS_REACHED (TYPE, EARNED_POINTS, PLAYER_NAME)我需要写一个给我排行榜的查询。
球员可以进入排行榜,当且仅当他与GROUP='IMPORTANT'达到至少一个目标。
例如:
+--------------+
| PLAYER |
+--------------+
| John |
| Bill |
| Bob |
| Willy |
+--------------+
+-------------------+
| ALL_GOALS |
+-------------------+
| A, IMPORTANT, 100 |
| B, IMPORTANT, 200 |
| C, OTHER, 10 |
| D, OTHER, 10 |
+-------------------+
+--------------+
| GOALS_REACHED|
+--------------+
| A, 100, John |
| B, 200, Willy|
| C, 10, Bob |
| D, 10, Bob |
+--------------+我只想在我的排行榜上看到约翰和威利
发布于 2021-10-14 18:26:24
像这样的东西?(第1-13行的样本数据;查询从第14行开始)
SQL> with
2 all_goals (type, cgroup, points) as
3 (select 'A', 'IMPORTANT', 100 from dual union all
4 select 'B', 'IMPORTANT', 200 from dual union all
5 select 'C', 'OTHER' , 10 from dual union all
6 select 'D', 'OTHER' , 10 from dual
7 ),
8 goals_reached (type, earned_points, player_name) as
9 (select 'A', 100, 'John' from dual union all
10 select 'B', 200, 'Willy' from dual union all
11 select 'C', 10, 'Bob' from dual union all
12 select 'D', 10, 'Bob' from dual
13 )
14 select r.player_name
15 from goals_reached r join all_goals g on g.type = r.type
16 where g.cgroup = 'IMPORTANT'
17 and g.points > 0;
PLAYE
-----
John
Willy
SQL>发布于 2021-10-15 07:36:13
感谢@Littlefoot answer和@mathguy有用的评论,我改进了查询并基于它创建了一个视图。这个看起来最像是一个无序的排行榜。
select gr1.PLAYER_NAME, sum(gr1.EARNED_POINTS)
from GOALS_REACHED gr1
where exists(
select *
from GOALS_REACHED gr2
inner join ALL_GOALS ag
on gr2.TYPE=ag.TYPE
where ag.GROUP='IMPORTANT'
and gr1.PLAYER_NAME=gr2.PLAYER_NAME
)
group by gr1.PLAYER_NAMEhttps://stackoverflow.com/questions/69575561
复制相似问题