-1

I have a table in oracle 11g like this:

id   date  
---  ---
1    1-jun
1    2-jun
1    3-jun
2    1-jul
2    2-jul
2    3-jul

I am trying to extract the latest record corresponding to each id. I tried group by, max but I cannot get it to work. What I want is:

   id    date 
   ---   ---
    1    3-jun
    2    3-jul
Victor
  • 16,609
  • 71
  • 229
  • 409
  • 1
    What is the column type for your date? `SELECT id, MAX(date) FROM table GROUP BY id` works for me. – Kermit Jul 11 '12 at 19:29
  • It is varchar2 but when i do: SELECT id, MAX(to_date(date,'yyyy-mm-dd') FROM table GROUP BY id; i get multiple records per id. Not the latest one. – Victor Jul 11 '12 at 20:35
  • I found it here: http://stackoverflow.com/questions/612231/how-can-i-select-rows-with-maxcolumn-value-distinct-by-another-column-in-sql Thank you for your replies – Victor Jul 11 '12 at 20:47

2 Answers2

6

Try this:

SELECT id, MAX(date)
  FROM <YOUR-TABLE>
 GROUP BY id
Chandu
  • 81,493
  • 19
  • 133
  • 134
0

try this one

'SELECT *FROM (SELECT * FROM table ORDER BY date   DESC )tmp GROUP BY Id ';
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130