tags:

views:

441

answers:

6

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          |
+13  A: 
SELECT col1, MAX(col2) FROM some_table GROUP BY col1;
Milen A. Radev
+4  A: 
 select col1, max(col2)
 from table
 group by col1
Tom Ritter
A: 

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

Quassnoi
I think you mean "select other columns .."
David Aldridge
Sure I do, thanks
Quassnoi
+2  A: 

SELECT Col1, MAX(Col2) FROM YourTable GROUP BY Col1

KM
+3  A: 

i reckon it would be

select col1, max(col2) from DemoTable group by col1

unless i've missed something obvious

Tim Brown
+3  A: 
select col1, max(col2) from MyTable
group by col1
Gustavo