2

I'm trying to return a list of years when certain conditions are met but I am having trouble with the MAX function and having it work with the rest of my logic.

For the following two tables:

          coach
coach | team | wins | year
------+------+------+------
nk01  | a    | 4    | 2000
vx92  | b    | 1    | 2000
nk01  | b    | 5    | 2003
vx92  | a    | 2    | 2003

           team
team | worldcupwin | year
-----+-------------+------
a    | Y           | 2000
b    | N           | 2000
a    | Y           | 2003
b    | N           | 2003

I want to get the following output:

years
-----
2000

Where the years printed are where the coaches' team with most wins during that year also won the world cup.

I decided to use the MAX function but quickly ran into the problem of not knowing how to use it to only be looking for max values for a certain year. This is what I've got so far:

SELECT y.year
FROM (SELECT c.year, MAX(c.wins), c.team
      FROM coach AS c
      WHERE c.year >= 1999
      GROUP BY c.year, c.team) AS y, teams AS t
WHERE y.year = t.year AND t.worldcupwin = 'Y' AND y.team = t.team;

This query outputs all years greater than 1999 for me, rather than just those where a coach with the most wins also won the world cup. (Using postgresql)

Any help is appreciated!

4 Answers 4

1

You can use correlated subquery

DEMO

   SELECT c.year, c.team
      FROM coachs AS c inner join teams t on c.team = t.team and c.year=t.year
      WHERE c.year >= 1999 and exists (select 1 from coachs c1 where c.team=c1.team 
      having max(c1.wins)=c.wins) 

and t.worldcupwin = 'Y'

OUTPUT:

year    team
2000    a 
Sign up to request clarification or add additional context in comments.

8 Comments

What does the exists do here? I am narrowing down some answers but not all of them are correct.
it will check whether the team has max win or not @Wintress
I see. Is this condition checking whether it's max win for that specific year? Or in general?
do you need it year wise or in general @Wintress
Ok, this partially works but is ignoring the last part t.worldcupwin = 'Y', since I'm selecting from c.year, & c.team. So even when t.worldcupwin is N, it will still output a year. I also need it year wise! fa
|
1

The following query uses DISTINCT ON:

SELECT DISTINCT ON (year) c.year, wins, worldcupwin, c.team
FROM coach AS c
INNER JOIN team AS t ON c.team = t.team AND c.year = t.year 
WHERE c.year > 1999 
ORDER BY year, wins DESC

in order to return the records having the biggest number of wins per year

year    wins    worldcupwin team
---------------------------------
2000    4       Y           a
2003    5       N           b

Filtering out teams that didn't win the world cup:

SELECT year, team
FROM (
   SELECT DISTINCT ON (year) c.year, wins, worldcupwin, c.team
   FROM coach AS c
   INNER JOIN team AS t ON c.team = t.team AND c.year = t.year 
   WHERE c.year > 1999 
   ORDER BY year, wins DESC) AS t
WHERE t.worldcupwin = 'Y'       
ORDER BY year, wins DESC

gives the expected result:

year    team
-------------
2000    a

Demo here

Comments

1

You can use the below to get the desired result:

EASY METHOD

SELECT TOP 1 c.year 

FROM coach AS c INNER JOIN team AS t ON c.team = t.team AND c.year = t.year

WHERE t.worldcupwin = 'Y'

ORDER BY c.wins DESC;

Comments

0

use row_number() window function

select a.coach,a.team,a.win,a.year from 

 (select c.*,t.*,
row_number()over(order by wins desc) rn 
from  coach c join team t on c.team=t.team
 where worldcupwin='Y'
 ) a where a.rn=1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.