Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7574581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:28:14+00:00 2026-05-30T16:28:14+00:00

I have a query that has the following DATE ID Name — ———— ———–

  • 0

I have a query that has the following

DATE                    ID              Name
---                     ------------    -----------
2012-02-07 11:24:53.000 00001-KK-12 Smith, JEN  
2011-12-28 00:00:00.000 00001-KK-12 Bearson, Matt   
2012-02-13 10:38:18.000 00003-KJ-12 Wick, Julian    

What I need to do is to get the latest date for a given ID and then show the results

So in this case, it would be:

DATE                    ID              Name
---                     ------------    -----------
2012-02-07 11:24:53.000 00001-KK-12 Smith, JEN  
2012-02-13 10:38:18.000 00003-KJ-12 Wick, Julian    

I tried to use the Top(1) with a group by on ID based but was not successful

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:28:15+00:00Added an answer on May 30, 2026 at 4:28 pm

    There are several ways to do this. One way is to use row_number. Its useful if there’s a possibility that there’s a tie on date and you want to arbitrarily pick one.

    WITH CTE AS (
    SELECT 
       row_number() over (partition by id order by date desc) rn,
       date,
       id,
       name
    
    FROM
       table)
    SELECT date,
           id,
           name
    FROM CTE WHERE RN = 1
    

    Another option is to use an ANTI JOIN (no aggregates no CTE) as follows but will return multiple results if there’s a tie for first for a given ID.

       SELECT 
          t.date,
          t.id,
          t.name
       FROM
          table t
          LEFT JOIN table t1
          WHERE t.Id = t1.id
                and t.Date < t1.Date
       WHERE
           t1.Date is null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sql query : SELECT DATE(procedures.start) date, name, COUNT(procedure_types.id) count FROM
I have a query that has 7 inner joins (because a lot of the
I have an sql query that has a parameter that can be null in
I have this query statement and want to only get records that has a
I have objects that has a DateTime property, how can i query for the
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
Hi all I have an appointments table that has among other fields a DATE
I have a Database that has the following relations: Transaction->Purchase->Item->Schedule Transaction - self explanitory
I am using in C# MYsql .I have query that works if I run
I have a query that is dynamically built after looking up a field list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.