-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-game-results.sql
More file actions
55 lines (51 loc) · 1.53 KB
/
Copy pathdebug-game-results.sql
File metadata and controls
55 lines (51 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- Query para debugar os resultados de cada jogo do Pedro Costa
WITH player_games AS (
-- Eventos que cada jogador jogou (está em um time do evento finalizado)
SELECT DISTINCT
tm.user_id,
t.event_id
FROM team_members tm
INNER JOIN teams t ON tm.team_id = t.id
WHERE t.event_id IN (
SELECT id FROM events
WHERE group_id = 'aaaabbbb-cccc-dddd-eeee-111111111111'
AND status = 'finished'
)
),
game_results AS (
-- Resultado de cada jogo para cada jogador
SELECT
pg.user_id,
pg.event_id,
t_player.id as player_team_id,
-- Gols do time do jogador (direto, sem multiplicar por jogadores!)
(SELECT COUNT(*)
FROM event_actions ea
WHERE ea.team_id = t_player.id
AND ea.event_id = pg.event_id
AND ea.action_type = 'goal'
) as team_goals,
-- Gols do time adversário
(SELECT COUNT(*)
FROM event_actions ea
INNER JOIN teams t ON ea.team_id = t.id
WHERE t.event_id = pg.event_id
AND t.id != t_player.id
AND ea.action_type = 'goal'
) as opponent_goals
FROM player_games pg
INNER JOIN team_members tm ON tm.user_id = pg.user_id
INNER JOIN teams t_player ON t_player.id = tm.team_id AND t_player.event_id = pg.event_id
)
SELECT
gr.*,
e.starts_at,
CASE
WHEN gr.team_goals > gr.opponent_goals THEN 'WIN'
WHEN gr.team_goals < gr.opponent_goals THEN 'LOSS'
ELSE 'DRAW'
END as result
FROM game_results gr
INNER JOIN events e ON gr.event_id = e.id
WHERE gr.user_id = '33333333-3333-3333-3333-333333333333'
ORDER BY e.starts_at DESC;