Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select distinct rows

Tags:

sql

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          |
like image 969
laurie Avatar asked Nov 27 '22 13:11

laurie


2 Answers

SELECT col1, MAX(col2) FROM some_table GROUP BY col1;
like image 61
Milen A. Radev Avatar answered Dec 10 '22 02:12

Milen A. Radev


 select col1, max(col2)
 from table
 group by col1
like image 25
Tom Ritter Avatar answered Dec 10 '22 02:12

Tom Ritter