Selecting records based on last updated date/time field

  • Probably a simple question but new to this and struggling, so any help appreciated.

    I have a table which contains multiple records for each person, each record has a last updated field containing a datetime value.

    I need to create a report showing the person name and several other fields but only those records which correspond to the most recently updated record as defined by the last updated field.

    i.e. from the following data: -

    Name Location Colour DateTime

    Fred New York Blue 10/21/2009 11:02:03

    Fred Boston Red 10/27/2009 14:15:47

    Fred Chicago Green 10/24/2009 16:13:27

    My report would show - Fred Boston Red 10/27/2009 14:15:47 - as the last updated record.

    Any suggestions??

  • Give this a shot:

    with LatestInfo as (

    select

    [Name],

    max([DateTime]) as [DateTime]

    from

    dbo.MyTable

    group by

    [Name]

    )

    select

    mt.*

    from

    dbo.MyTable mt

    inner join LatestInfo li

    on (mt.[Name] = li.[Name]

    and mt.[DateTime] = li.[DateTime]);

  • Thanks Lynn

    Seems to be working ok (not sure why - but can figure that out I guess) just need to validate the data it's giving me.

    Thanks a million for the help:-D

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply