Results 1 to 4 of 4

Thread: SQL query - retreiving most recent record for an entity *resolved*

  1. #1
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 05
    Location
    Montana
    Posts
    2,820

    SQL query - retreiving most recent record for an entity *resolved*

    Say I've got the following table
    ---------------
    |1|2006-10-04|
    |1|2006-10-05|
    |2|2006-10-04|
    |2|2006-10-05|
    ---------------

    How could I write a SQL query to return the most recent record for each id? The result should look like this
    ---------------
    |1|2006-10-05|
    |2|2006-10-05|
    ---------------
    Last edited by wild_bill; Nov 2nd, 2006 at 10:13 AM.

  2. #2
    #28 for the Yanks coming GaryMazzone's Avatar
    Join Date
    Aug 05
    Location
    Dover,NH
    Posts
    6,900

    Re: SQL query - retreiving most recent record for an entity

    Maybe something like this:

    Code:
    "Select * From TableName Where ID IN (Select ID From TableName Where Date = '" & Select MAX(DateField) From TableName Where ID = (Select Distinct(ID) From TableName) & "')"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 05
    Location
    Montana
    Posts
    2,820

    Re: SQL query - retreiving most recent record for an entity

    I ended up using
    Code:
    SELECT submitter_id,max(created_date)
    FROM dbo.claim_stub WITH (NOLOCK)
    GROUP BY submitter_id
    Thanks for your response.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,628

    Re: SQL query - retreiving most recent record for an entity *resolved*

    You dont need the '" & or & "' around the inner Select.

    Here's an alternative method:
    Code:
    Select ID, Max([Date])
    From TableName 
    GROUP BY ID
    edit: oops, I arrived too late!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •