-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_codes.txt
146 lines (135 loc) · 3.94 KB
/
sample_codes.txt
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
145
146
i have some sample QUERIES .
1.batting satstics query
WITH
-- Filtered balls data
filtered_balls AS (
SELECT *
FROM bbbdata.ballsnew_2406
WHERE insert condition here
),
-- Intermediate innings data
innings AS (
SELECT
match_no ,
SUM(batter_runs)) AS runs -- use the column batter_runs for individuals , use score for calculating teams.
SUM(balls_faced) AS balls,
SUM(batter_out) AS outs,
SUM(CASE WHEN batter_runs = 4 THEN 1 ELSE 0 END) AS fours,
SUM(CASE WHEN batter_runs = 6 THEN 1 ELSE 0 END) AS sixes,
SUM(CASE WHEN score = 0 Then 1 else 0 end ) AS dots
FROM filtered_balls
GROUP BY batter/team_bat other that you feel neccesary to group by,
match_no
),
-- Calculate batting stats
batting_stats AS (
SELECT
count(distinct(match_no)) as innings,
SUM(runs) AS total_runs,
SUM(balls) AS total_balls,
SUM(outs) AS total_outs,
CASE
WHEN SUM(balls) = 0 THEN 1e308
ELSE SUM(runs) / SUM(balls) * 100
END AS Strike_Rate,
CASE
WHEN SUM(outs) = 0 THEN 1e308
ELSE SUM(runs) / SUM(outs)
END AS Average,
MAX(runs) AS highest_score,
SUM(CASE WHEN runs >= 50 AND runs < 100 THEN 1 ELSE 0 END) AS fifties,
SUM(CASE WHEN runs >= 100 THEN 1 ELSE 0 END) AS hundreds,
SUM(fours) AS total_fours,
SUM(sixes) AS total_sixes,
CASE
WHEN SUM(balls) = 0 THEN 1e308
ELSE SUM(dots) / SUM(balls) * 100
END AS dot_percent,
FROM innings
GROUP BY batter batter/team_bat other that you feel neccesary to group by,
)
SELECT
innings,
total_runs,
total_balls,
total_outs,
CASE
WHEN Average = 1e308 THEN NULL
ELSE ROUND(Average, 2)
END as Average,
CASE
WHEN Strike_Rate = 1e308 THEN null
ELSE ROUND(Strike_Rate, 2)
END AS Strike_Rate,
CASE
WHEN dot_percent = 1e308 THEN null
ELSE ROUND(dot_percent, 2)
END AS Dot_Percentage,
highest_score AS hs,
fifties,
hundreds,
total_fours,
total_sixes,
FROM batting_stats;
2.bowling stats query
WITH
-- Filtered balls data
filtered_balls AS (
SELECT *
FROM bbbdata.ballsnew_2406
WHERE insert condition here
),
-- Intermediate bowling data
bowling AS (
SELECT
bowler/team_bowl or other u need ,
match_no,
SUM(batter_out) AS wickets,
SUM(bowler_runs) AS runs_conceded,
SUM(balls_faced) AS balls_bowled,
SUM(CASE WHEN score = 0 THEN 1 ELSE 0 END) AS dot_balls,
SUM(CASE WHEN batter_runs IN (4, 6) THEN 1 ELSE 0 END) AS boundary_balls
FROM filtered_balls
GROUP BY bowler/team_bowl, match_no
),
-- Calculate bowling stats
bowling_stats AS (
SELECT
bowler,
COUNT(DISTINCT match_no) AS innings,
SUM(wickets) AS total_wickets,
SUM(runs_conceded) AS total_runs_conceded,
SUM(balls_bowled) AS total_balls_bowled,
SUM(dot_balls) AS total_dot_balls,
SUM(boundary_balls) AS total_boundary_balls,
CASE
WHEN SUM(wickets) = 0 THEN NULL
ELSE SUM(runs_conceded) / SUM(wickets)
END AS average,
CASE
WHEN SUM(balls_bowled) = 0 THEN NULL
ELSE (SUM(runs_conceded) / SUM(balls_bowled)) * 6
END AS economy_rate,
CASE
WHEN SUM(balls_bowled) = 0 THEN NULL
ELSE (SUM(dot_balls) / SUM(balls_bowled)) * 100
END AS dot_ball_percentage,
CASE
WHEN SUM(balls_bowled) = 0 THEN NULL
ELSE (SUM(boundary_balls) / SUM(balls_bowled)) * 100
END AS boundary_percentage
FROM bowling
GROUP BY bowler/team_bowl, match_no
)
SELECT
bowler,
innings,
total_wickets,
total_runs_conceded,
total_balls_bowled,
ROUND(average, 2) AS average,
ROUND(economy_rate, 2) AS economy_rate,
ROUND(dot_ball_percentage, 2) AS dot_ball_percentage,
ROUND(boundary_percentage, 2) AS boundary_percentage
FROM bowling_stats
ORDER BY total_wickets DESC;