SQL selects individual rows

I have data like this (col2 is of type Date)

  |  col1 |  col2 |
 ------------------------------
 |  1 |  10/17/2007 07:19:07 |
 |  1 |  10/17/2007 07:18:56 |
 |  1 |  31/12/2070 |
 |  2 |  11/28/2008 3:23:14 PM |
 |  2 |  31/12/2070 |

How to select rows that col1 are different and col2 is the largest. Like this

  |  col1 |  col2 |
 ------------------------------
 |  1 |  31/12/2070 |
 |  2 |  31/12/2070 |
+4
source share
6 answers
SELECT col1, MAX(col2) FROM some_table GROUP BY col1; 
+16
source
  select col1, max(col2) from table group by col1 
+4
source

I believe it will be

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

if I did not miss something obvious

+3
source
 select col1, max(col2) from MyTable group by col1 
+3
source

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

+2
source

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

0
source

All Articles