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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:52:59+00:00 2026-05-16T14:52:59+00:00

I have a table with many IDs and many dates associated with each ID,

  • 0

I have a table with many IDs and many dates associated with each ID, and even a few IDs with no date. For each ID and date combination, I want to select the ID, date, and the next largest date also associated with that same ID, or null as next date if none exists.

Sample Table:

ID      Date
1       5/1/10
1       6/1/10
1       7/1/10
2       6/15/10
3       8/15/10
3       8/15/10
4       4/1/10
4       4/15/10
4       

Desired Output:

ID       Date       Next_Date
1        5/1/10     6/1/10
1        6/1/10     7/1/10
1        7/1/10     
2        6/15/10    
3        8/15/10    
3        8/15/10    
4        4/1/10     4/15/10
4        4/15/10    
  • 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-16T14:52:59+00:00Added an answer on May 16, 2026 at 2:52 pm
    SELECT
        mytable.id,
        mytable.date,
        (
            SELECT
                MIN(mytablemin.date)
            FROM mytable AS mytablemin
            WHERE mytablemin.date > mytable.date
                AND mytable.id = mytablemin.id
        ) AS NextDate
    FROM mytable
    

    This has been tested on SQL Server 2008 R2 (but it should work on other DBMSs) and produces the following output:

    id          date                    NextDate
    ----------- ----------------------- -----------------------
    1           2010-05-01 00:00:00.000 2010-06-01 00:00:00.000
    1           2010-06-01 00:00:00.000 2010-06-15 00:00:00.000
    1           2010-07-01 00:00:00.000 2010-08-15 00:00:00.000
    2           2010-06-15 00:00:00.000 2010-07-01 00:00:00.000
    3           2010-08-15 00:00:00.000 NULL
    3           2010-08-15 00:00:00.000 NULL
    4           2010-04-01 00:00:00.000 2010-04-15 00:00:00.000
    4           2010-04-15 00:00:00.000 2010-05-01 00:00:00.000
    4           NULL                    NULL
    

    Update 1:
    For those that are interested, I’ve compared the performance of the two variants in SQL Server 2008 R2 (one uses MIN aggregate and the other uses TOP 1 with an ORDER BY):

    Without an index on the date column, the MIN version had a cost of 0.0187916 and the TOP/ORDER BY version had a cost of 0.115073 so the MIN version was “better”.

    With an index on the date column, they performed identically.

    Note that this was testing with just these 9 records so the results could be (very) spurious…

    Update 2:
    The results hold for 10,000 uniformly distributed random records. The TOP/ORDER BY query takes so long to run at 100,000 records I had to cancel it and give up.

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

Sidebar

Related Questions

I have a many to many index table, and I want to do an
I have a table which is referenced by foreign keys on many other tables.
I have a table called BlogPost which has a 1-to-many relationship with the Comment
I have many tables in my database which are interrelated. I have a table
I have a fixed width DIV containing a table with many columns, and need
Right now I have a table called Campaigns that has many Hits, if I
I have a one to many relationship between two tables. The many table contains
I have a table that has 8 million records, with many fields, including lat/long
I have following situation. A main table and many other tables linked together with
I have a rather large (many gigabytes) table of data in SQL Server that

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.