-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaggregate_functions.sql
111 lines (101 loc) · 4.96 KB
/
aggregate_functions.sql
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
COUNT
-- Top 50 players with the most wickets
SELECT p.name, COUNT(*) AS wickets
FROM testruns
JOIN players AS p
ON testruns.bowler_id = p.id
GROUP BY p.name
ORDER BY wickets DESC
LIMIT 50
-- Matches that were umpired by Marais Erasmus
SELECT * FROM match
JOIN umpires AS u1
ON match.umpirea_id = u1.id
JOIN umpires AS u2
ON match.umpireb_id = u2.id
WHERE u1.name = 'Marais Erasmus'
OR u2.name = 'Marais Erasmus'
--Wickets made by All rounders
SELECT p.name, COUNT(*) AS wickets
FROM testruns t
JOIN players p
ON t.bowler_id = p.id
JOIN roles r
ON p.role_id = r.id
WHERE role = 'All-rounder'
GROUP BY p.name
ORDER BY wickets DESC
--All rounders with the most runs
SELECT p.name, SUM(runs) AS total_runs
FROM testruns t
JOIN players p
ON t.player_id = p.id
JOIN roles r
ON p.role_id = r.id
WHERE role = 'All-rounder'
GROUP BY p.name
ORDER BY total_runs DESC
-- Wicketkeepers with the most runs
SELECT p.name, SUM(runs) AS total_runs
FROM testruns t
JOIN players p
ON t.player_id = p.id
JOIN roles r
ON p.role_id = r.id
WHERE role = 'Wicket-keeper'
GROUP BY p.name
ORDER BY total_runs DESC
-- 50 players who have played the most test matches during the tournament
SELECT p.name, COUNT(*) AS matches
FROM testruns
JOIN players AS p
ON testruns.player_id = p.id
GROUP BY p.name
ORDER BY matches
LIMIT 50
--Number of ducks made in a test match
SELECT p.name, runs, COUNT(*) AS ducks
FROM testruns
JOIN players AS p
ON testruns.player_id = p.id
WHERE runs= 0
AND wicket_id != 6
GROUP BY p.name, runs
ORDER BY COUNT(*) DESC
SUM
-- Top 50 players with the most runs
SELECT p.name, SUM(runs) AS total_runs
FROM testruns
JOIN players AS p
ON testruns.player_id = p.id
WHERE runs IS NOT NULL
GROUP BY p.name
ORDER BY total_runs DESC
LIMIT 50
--Number of balls faced in descending order
SELECT p.name, SUM(balls) AS total_balls
FROM testruns
JOIN players AS p
ON testruns.player_id = p.id
WHERE runs IS NOT NULL
GROUP BY p.name
ORDER BY total_balls DESC
LIMIT 50
--Most Boundaries by a player in the tournament
SELECT p.name, SUM(fours + sixes) AS total_boundaries
FROM testruns
JOIN players AS p
ON testruns.player_id = p.id
WHERE runs IS NOT NULL
GROUP BY p.name
ORDER BY total_boundaries DESC
LIMIT 50
MAX
-- Most runs scored in an innings in the tournament
SELECT p.name, MAX(runs) AS maxruns
FROM testruns AS t
JOIN players AS p
ON t.player_id = p.id
WHERE runs IS NOT NULL
GROUP BY t.match_id, p.name
ORDER BY maxruns DESC