2009-03-27 7 views
4

J'ai données comme celui-ci (col2 est de type Date)SQL sélectionner des lignes distinctes

 
| 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   | 

Comment sélectionnerait les lignes qui col1 est distincte et la valeur de col2 est le plus grand. Comme cette

 
| col1 |  col2   | 
------------------------------ 
| 1 | 31/12/2070   | 
| 2 | 31/12/2070   | 

Répondre

16
SELECT col1, MAX(col2) FROM some_table GROUP BY col1; 
4
select col1, max(col2) 
from table 
group by col1 
0

En Oracle et MS SQL:

SELECT * 
FROM (
     SELECT t.*, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2 DESC) rn 
     FROM table t 
     ) q 
WHERE rn = 1 

Cela permet de sélectionner d'autres colonnes avec col1 et col2

+0

Je pense que vous voulez dire « choisir d'autres colonnes .. " –

+0

Bien sûr, je le fais, merci – Quassnoi

2

SELECT Col1, MAX (Col2) DE VotreTable GROUP BY Col1

3

je pense que ce serait

select col1, max (col2) de DemoTable groupe par col1

à moins que je l'ai raté quelque chose d'évident

3
select col1, max(col2) from MyTable 
group by col1 
Questions connexes