Spirit
Spirit

Reputation: 43

SELECT a variable as condition and display another one

My problem is the following.

I want to SELECT the minimum of a years list and display another row of my table.

An example:

SELECT MIN(Year) 
FROM table     -> Searching for the lowest year.

and then I want it to display the Winners of the first year.

Is there a way to do this in just one line?

Upvotes: 0

Views: 57

Answers (3)

jean
jean

Reputation: 4350

Try it

select t.columntoshow, min(t.year) from table t group by t.columntoshow

Upvotes: 0

Konstantin
Konstantin

Reputation: 3294

select winner from table where year in (select min(year) from table)

Upvotes: 1

LSerni
LSerni

Reputation: 57418

You need to do a self-JOIN between table and itself (I suppose you want to do it in a single statement, not a single line):

SELECT A.*
    FROM table AS A
    JOIN ( SELECT MIN(Year) AS Year FROM table ) AS B
    ON (A.Year = B.Year);

This assumes that there is only one record per minimum-year in every group of interest.

Upvotes: 0

Related Questions