get the latest DATE

  • How do you get firstname, lastname for the latest DATE?

    Thanks.

    create table computert1 (id int identity (1,1), firstname varchar(20), lastname varchar(20))

    insert computert1 (firstname, lastname)

    select 'daniel', 'wade' union all

    select 'joe', 'walker' union all

    select 'daniel', 'serious' union all

    select 'sam', 'jordan'

    create table computert2 (id int identity (1,1), dates datetime)

    insert computert2 (dates)

    select getdate() union all

    select '2008-10-01' union all

    select '2005-10-01'

  • DECLARE @Date DATETIME

    SELECT @Date = MAX(dates) FROM computert2

    SELECT FirstName, Lastname, Dates As LatestDate

    FROM computert1

    INNER JOIN computert2

    ON computert1.id = computert2.id

    WHERE dates = @Date

    -Lucky

  • Without variable

    SELECT FirstName, Lastname, Dates As LatestDate

    FROM computert1

    INNER JOIN computert2

    ON computert1.id = computert2.id

    WHERE dates = (SELECT MAX(dates) FROM computert2)

    -Lucky

  • SELECT TOP 1*

    FROM

    (SELECT FirstName, Lastname, Dates As LatestDate

    FROM computert1

    INNER JOIN computert2

    ON computert1.id = computert2.id)tmp

    ORDER BY tmp.LatestDate desc

  • Thank you so much.

Viewing 5 posts - 1 through 4 (of 4 total)

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