-
Notifications
You must be signed in to change notification settings - Fork 1
/
130_write a query to digitize a result table (2 methods).sql
80 lines (70 loc) · 1.78 KB
/
130_write a query to digitize a result table (2 methods).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
use int_ques;
/*
Write a SQL query to display total number of matches played, matches won,
matches tied and matches lost for each team
*/
/*
create table match_result(
Team_1 varchar(20),
Team_2 varchar(20),
Result_won varchar(20)
)
insert into match_result(team_1, team_2, Result_won)
values('India','Australia','India'),
('India','England','England'),
('SouthAfrica','India','India'),
('Australia','England',NULL),
('England','SouthAfrica','SouthAfrica'),
('Australia','India','Australia')
*/
--select * from match_result;
--Method 1
with t1 as(
select team_1,
team_2,
result_won,
case when team_1 = result_won then 1 end as team_1_won,
case when team_2 = result_won then 1 end as team_2_won,
case when result_won is null then 1 end as tied_match
from match_result
),
team_1_result as(
select team_1, sum(team_1_won) as won_match, sum(team_2_won) as lost_match,
sum(tied_match) as tied
from t1
group by team_1
),
team_2_result as(
select team_2, sum(team_2_won) as won_match, sum(team_1_won) as lost_match,
sum(tied_match) as tied
from t1
group by team_2),
t3 as(
select * from team_1_result
union all
select * from team_2_result
),result as(
select team_1 as team, sum(won_match) as won_match,
sum(lost_match) as lost_match,
sum(tied) as tied
from t3
group by team_1
)
select *,
isnull(won_match,0) + isnull(lost_match,0) + isnull(tied,0) as match_played
from result
--Method 2
with t1 as(
select team_1 as team, result_won as result
from match_result
union all
select team_2 as team, result_won as result
from match_result
)
select team,
count(1) as match_count,
sum(case when team = result then 1 end) as match_won,
sum(case when result is null then 1 end) as tied_match,
sum(case when team != result then 1 end) as lost_match
from t1
group by team