-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSL.sql
More file actions
144 lines (99 loc) · 2.95 KB
/
PSL.sql
File metadata and controls
144 lines (99 loc) · 2.95 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
--highest scorer in the match
select top 1
striker, sum(runs_off_bat) as scores
from psl
where match_id = '1247036'
group by
striker
order by
scores
desc
---highest wicket taker in the match
select top 1
count(player_dismissed) as player_out ,bowler
from PSL
where match_id = '1247036'
group by bowler
order by player_out
desc
--who won the match?
select
match_id,
MAX(team_score) AS team_score,
MAX(CASE WHEN team_score = max_score THEN batting_team END) AS winning_team
FROM
(SELECT
match_id,
batting_team,
SUM(runs_off_bat + extras) AS team_score,
MAX(SUM(runs_off_bat + extras)) OVER (PARTITION BY match_id) AS max_score
FROM
PSL
WHERE
innings IN ('1', '2')
GROUP BY
match_id, batting_team) AS subquery
GROUP BY
match_id;
---inserting data into a newly created table
insert into team_won (match_id , team_score, winning_team)
select
match_id,
MAX(team_score) AS team_score,
MAX(CASE WHEN team_score = max_score THEN batting_team END) AS winning_team
FROM
(SELECT
match_id,
batting_team,
SUM(runs_off_bat + extras) AS team_score,
MAX(SUM(runs_off_bat + extras)) OVER (PARTITION BY match_id) AS max_score
FROM
PSL
WHERE
innings IN ('1', '2')
GROUP BY
match_id, batting_team) AS subquery
GROUP BY
match_id;
---Total wins by teams in each PSL season (1-7)
select *
from PSL as t1
inner join team_won as t2
on t1.match_id = t2.match_id
select t2.match_id , t1.season, count(distinct t2.winning_team) as total_wins , t2.winning_team
from PSL as t1
inner join team_won as t2
on t1.match_id = t2.match_id
group by t2.match_id,t1.season,t2.winning_team
order by t2.winning_team
--Top striker in winnnig team in each match(from s1 to s8)
WITH CTE AS (
SELECT t2.winning_team, t1.striker, SUM(t1.runs_off_bat) AS total_runs, t1.date_only,
ROW_NUMBER() OVER (PARTITION BY t1.match_id ORDER BY SUM(t1.runs_off_bat) DESC) AS rn
FROM PSL AS t1
INNER JOIN team_won AS t2 ON t1.match_id = t2.match_id
WHERE t1.date_only = date_only
GROUP BY t2.winning_team, t1.striker, t1.date_only, t1.match_id
)
SELECT winning_team, striker, total_runs, date_only
FROM CTE
WHERE rn = 1
---top three grounds with highest sixes in all seasons (1-7)
select top 3
venue, runs_off_bat, count(venue) as total_sixes
from PSL
where runs_off_bat = '6'
group by
venue , runs_off_bat
order by total_sixes desc
---highest sixes in a ground in each season
with CTE as ( select
venue, season, runs_off_bat ,
count(runs_off_bat) over(partition by season) as total_sixes,
row_number() over(partition by season order by season desc) as r_n
from PSL
where runs_off_bat = '6'
)
select venue, season, runs_off_bat, total_sixes, r_n
from CTE
where r_n = '1'