Reputation: 6251
I have data like this (col2 is of type Date)
| col1 | col2 | ------------------------------ | 1 | 17/10/2007 07:19:07 | | 1 | 17/10/2007 07:18:56 | | 1 | 31/12/2070 | | 2 | 28/11/2008 15:23:14 | | 2 | 31/12/2070 |
How would select rows which col1 is distinct and the value of col2 is the greatest. Like this
| col1 | col2 | ------------------------------ | 1 | 31/12/2070 | | 2 | 31/12/2070 |
Upvotes: 4
Views: 2834
Reputation: 425753
In Oracle
and MS SQL
:
SELECT *
FROM (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2 DESC) rn
FROM table t
) q
WHERE rn = 1
This will select other columns along with col1
and col2
Upvotes: 0
Reputation: 6277
i reckon it would be
select col1, max(col2) from DemoTable group by col1
unless i've missed something obvious
Upvotes: 3