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 6033995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:33:55+00:00 2026-05-23T05:33:55+00:00

Assume the table of records below ID Name AppointmentDate — ——– ————— 1 Bob

  • 0

Assume the table of records below

ID    Name       AppointmentDate
--    --------   ---------------
1     Bob         1/1/2010
1     Bob         5/1/2010
2     Henry       5/1/2010
2     Henry       8/1/2011
3     John        8/1/2011
3     John       12/1/2011

I want to retrieve the most recent appointment date by person. So I need a query that will give the following result set.

1   Bob    5/1/2010 (5/1/2010 is most recent)
2   Henry  8/1/2011 (8/1/2011 is most recent)
3   John   8/1/2011 (has 2 future dates but 8/1/2011 is most recent)

Thanks!

  • 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-23T05:33:56+00:00Added an answer on May 23, 2026 at 5:33 am

    Assuming that where you say “most recent” you mean “closest”, as in “stored date is the fewest days away from the current date and we don’t care if it’s before or after the current date”, then this should do it (trivial debugging might be required):

    SELECT ID, Name, AppointmentDate
     from (select
               ID
              ,Name
              ,AppointmentDate
              ,row_number() over (partition by ID order by abs(datediff(dd, AppointmentDate, getdate()))) Ranking
             from MyTable) xx
     where Ranking = 1
    

    This usese the row_number() function from SQL 2005 and up. The subquery “orders” the data as per the specifications, and the main query picks the best fit.

    Note also that:

    • The search is based on the current date
    • We’re only calculating difference in days, time (hours, minutes, etc.) is ignored
    • If two days are equidistant (say, 2 before and 2 after), we pick one randomly

    All of which could be adjusted based on your final requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets assume I get an HTML table as a string, and I want -
Assume a table named 'log', there are huge records in it. The application usually
I have a table (natomr) with 200 records which defines different areas. I want
I have this homework question: Assume a Database table has 0 records at time
Assume that there is a table EMPLOYEE with columns ID (int), Name (nvarchar), Surname(nvarchar)
Assume the following table records: TABLE: foo ========================== | foo_id | foo_parent_id | ==========================
I need to select rows randomly from an Oracle DB. Ex: Assume a table
Assume I have a table called table and I have 3 columns, a, b,
Assume I have a table like this: column A(int) column B(int) 1 2 2
Assume I have two tables: Table MY_ENTITY ID: PK OTHER_ID: FK to table OTHER

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.