MySQL Forums
Forum List  »  General

Query Help
Posted by: Richard Dunnivan
Date: October 06, 2014 02:26PM

I have the following tables ...

gameInfo
+------------+
| Field |
+------------+
| gameID |
| year |
| week |
| homeTeamID |
| homeScore |
| awayTeamID |
| awayScore |
+------------+

teamInfo
+----------+
| Field |
+----------+
| teamID |
| coachID |
| teamName |
+----------+

coachInfo
+-----------+
| Field |
+-----------+
| coachID |
| userName |
| coachName |
+-----------+

I'm trying to write a query that will show which coach (or coaches) has won the most number of championships (always week 16)

I have the following that works, but it's very clunky. Is there an easier way to achieve this?


select *
from
(
select coachName, count(*) as championships
from
(
select if(homeScore > awayScore, homeTeamID, awayTeamID) as teamID
from gameInfo
where week = 16
) tab1
left join teamInfo using (teamID)
left join coachInfo using (coachID)
group by coachName
order by championships desc
) tab2
where championships =
(
select max(championships)
from
(
select coachName, count(*) as championships
from
(
select if(homeScore > awayScore, homeTeamID, awayTeamID) as teamID
from gameInfo
where week = 16
) tab3
left join teamInfo using (teamID)
left join coachInfo using (coachID)
group by coachName
order by championships desc
) tab4
)

Options: ReplyQuote


Subject
Written By
Posted
Query Help
October 06, 2014 02:26PM
October 10, 2014 12:10PM
November 19, 2014 12:49PM
November 20, 2014 11:30AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.