1

I have a table:

ID  Title   Date
30   1     10/10/2010
20   1     10/11/2010
40   2     01/01/2010

i need a way to select distinct titles with max dates.
so when title is 1, i should have the second row selected since the date in the 1st row is smaller than the date in the second row.
as a result the query should return:

20 1 10/11/2010
40 2 01/01/2010
Brandon Montgomery
  • 6,924
  • 3
  • 48
  • 71
scatman
  • 14,109
  • 22
  • 70
  • 93

3 Answers3

2
select max(ID) as ID, max(Date), Title
from table
group by Title

should do the trick

Pavel Urbančík
  • 1,466
  • 9
  • 6
  • 1
    This is valid for the data sample but breaks when the max date isn't on the row with the max id – Robb Nov 30 '10 at 13:12
  • but its not always true that the largest id have the largest date.... i will edit the table... – scatman Nov 30 '10 at 13:12
2

Your data set is unclear; do you want the minimum ID and the maximum date?

SELECT ID=MIN(ID), Title, [Date] = MAX([Date])
FROM Table
GROUP By Title

Or was it a typo, and you want the row with the maximum date (assuming that the dates may not follow the ID value)?

SELECT  a.ID
      , a.Title
      , a.Dt
FROM    Table a
        JOIN ( SELECT   b.Title
                      , Dt = MAX(Dt)
               FROM     Table b
               GROUP BY Title
             ) c ON a.Dt = c.Dt
                    AND a.Title = c.Title
Stuart Ainsworth
  • 12,792
  • 41
  • 46
0
select
max(ID), Title , max(Date)
from
table
group by
Title
Adam Flanagan
  • 3,052
  • 23
  • 31