Reputation: 1683
I want to find the minimum of a date column, where another condition is met
i.e with table :
ID date condition ...
1 3/15 0
1 4/15 1
5123 5/15 1
5123 7/15 1
5123 1/15 0
2 2/15 0
2 1/15 1
2 1/15 1
I want a single line within the select statement, without filtering the rest of the table, to produce :
ID date
1 4/15
5123 5/15
2 1/15
I have tried :
min( date with condition = 1 )
min( date where condition = 1 )
case when condition=1 then min(date) end
but am getting a syntax error.
Upvotes: 0
Views: 749
Reputation: 44766
"I don't want to filter the rest of the table", use a case
expression to do conditional aggregation:
select id, min(case when condition = 1 then date end)
from tablename
group by id
Upvotes: 2
Reputation: 13237
For each Id if you want the minimum date, you need to use the GROUP BY
SELECT ID, MIN(Date) AS Date
FROM TableName
WHERE Condition = 1
GROUP BY ID
Upvotes: 0