Wintress
Wintress

Reputation: 67

SQL Max Value for a Specified Limit

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!

Upvotes: 2

Views: 477

Answers (4)

Jithin Joy
Jithin Joy

Reputation: 136

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;

Upvotes: 1

Giorgos Betsos
Giorgos Betsos

Reputation: 72165

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

Upvotes: 1

Fahmi
Fahmi

Reputation: 37473

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 

Upvotes: 1

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 32003

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

Upvotes: 0

Related Questions