CREATE TABLE cricket (
match_no NUMBER,
team_a CHAR(30),
team_b CHAR(30),
winner CHAR(30)
);
insert into cricket values(01,'WESTINDIES','SRILANKA','WESTINDIES');
insert into cricket values(02,'INDIA','SRILANKA','INDIA');
insert into cricket values(03,'AUSTRALIA','SRILANKA','AUSTRALIA');
insert into cricket values(04,'WESTINDIES','SRILANKA','SRILANKA');
insert into cricket values(05,'AUSTRALIA','INDIA','AUSTRALIA');
insert into cricket values(06,'WESTINDIES','SRILANKA','WESTINDIES');
insert into cricket values(07,'INDIA','WESTINDIES','WESTINDIES');
insert into cricket values(08,'WESTINDIES','AUSTRALIA','AUSTRALIA');
insert into cricket values(09,'WESTINDIES','INDIA','INDIA');
insert into cricket values(10,'AUSTRALIA','WESTINDIES','WESTINDIES');
insert into cricket values(11,'WESTINDIES','SRILANKA','WESTINDIES');
insert into cricket values(12,'INDIA','AUSTRALIA','INDIA');
insert into cricket values(13,'SRILANKA','NEWZEALAND','SRILANKA');
insert into cricket values(14,'NEWZEALAND','INDIA','INDIA');
Query 1:
with matches_played as (select team_name, sum(cnt) as no_of_matches_played from(
select TEAM_A team_name,count(*) cnt
from cricket group by TEAM_A
union all
select TEAM_b,count(*) from cricket group by TEAM_b)
group by team_name),
matches_won as (select winner team_name,count(*) no_of_matches_won
from cricket
group by winner)
select matches_played.team_name, no_of_matches_played,nvl(no_of_matches_won,0) matches_won,no_of_matches_played-nvl(no_of_matches_won,0) matches_lost
from matches_played full outer join matches_won
on matches_played.team_name = matches_won.team_name;
Query 2:
select matches_played.team_name, no_of_matches_played,nvl(no_of_matches_won,0) matches_won,no_of_matches_played-nvl(no_of_matches_won,0) matches_lost
from (select team_name, sum(cnt) as no_of_matches_played from(
select TEAM_A team_name,count(*) cnt
from cricket group by TEAM_A
union all
select TEAM_b,count(*) from cricket group by TEAM_b)
group by team_name) matches_played,
(select winner team_name,count(*) no_of_matches_won
from cricket
group by winner)matches_won
where matches_played.team_name = matches_won.team_name(+);
Query 3:
with d as (select team_a a,
team_b b,
case when team_a = winner then 1 else 0 end a_w,
case when team_b = winner then 1 else 0 end b_w
from cricket)
select c,count(*) total,sum(w) won,count(*) -sum(w) lost from (
select decode(r,1,a,b) c,decode(r,1,a_w,b_w) w
from d,(select rownum r from dual connect by level <=2))
group by c;
Other way of writing the code:
ReplyDeleteselect team , count (team) matches_played, count(winner) matches_won , count (team) - count(winner) mathes_lost from
(
select team_a team, case when winner = team_a then team_a else null end winner from cricket
union all
select team_b team, case when winner = team_b then team_b else null end winner from cricket
)
group by team
This comment has been removed by the author.
ReplyDelete------OTHER WAY TO IMPLEMENT THIS-----
ReplyDeleteSELECT TEAMS,MATCHES_PLAYED,NVL(WON,0) WON,
(MATCHES_PLAYED - NVL(WON,0)) LOST FROM
(SELECT TEAMS, COUNT(*) MATCHES_PLAYED FROM
(SELECT TEAM_A TEAMS FROM CRICKET_DASHBOARD
UNION ALL
SELECT TEAM_B TEAMS FROM CRICKET_DASHBOARD)
GROUP BY TEAMS) A,
(SELECT WINNER, COUNT(*) WON FROM CRICKET_DASHBOARD
GROUP BY WINNER) B
WHERE A.TEAMS = B.WINNER(+);