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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:27:54+00:00 2026-05-14T02:27:54+00:00

In SQL Server 2008: I have two tables, dtlScheme and dtlRenewal, with a one

  • 0

In SQL Server 2008:

I have two tables, dtlScheme and dtlRenewal, with a one to many relationship (one scheme can have many renewals). dtlRenewal has a unique key (dteEffectiveDate, dtlSchemeID).

Now suppose I have the following data in dtlRenewal:

dtlRenewalID  dtlSchemeID   dteEffectiveDate
1             1             1/1/2005
2             1             1/1/2006
3             1             1/1/2007
4             1             1/1/2008
5             1             1/1/2009

I would like to find for each renewal the next and previous effective date for the scheme. In other words, I need to return this:

dtlRenewalID  dtlSchemeID   dteEffectiveDate  dtePrevious  dteNext
1             1             1/1/2005          NULL         1/1/2006
2             1             1/1/2006          1/1/2005     1/1/2007
3             1             1/1/2007          1/1/2006     1/1/2008
4             1             1/1/2008          1/1/2007     1/1/2009
5             1             1/1/2009          1/1/2008     NULL

Thanks

Karl

  • 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-14T02:27:54+00:00Added an answer on May 14, 2026 at 2:27 am

    try this:

    DECLARE @YourTable table (dtlRenewalID int, dtlSchemeID int, dteEffectiveDate datetime)
    SET NOCOUNT ON
    INSERT @YourTable VALUES (1,1,'1/1/2005')
    INSERT @YourTable VALUES (2,1,'1/1/2006')
    INSERT @YourTable VALUES (3,1,'1/1/2007')
    INSERT @YourTable VALUES (4,1,'1/1/2008')
    INSERT @YourTable VALUES (5,1,'1/1/2009')
    INSERT @YourTable VALUES (6,2,'1/1/2005') --I just repeated the data to make sure 
    INSERT @YourTable VALUES (7,2,'1/1/2006') --it would work with multiple dtlSchemeID
    INSERT @YourTable VALUES (8,2,'1/1/2007') --values, which it does
    INSERT @YourTable VALUES (9,2,'1/1/2008')
    INSERT @YourTable VALUES(10,2,'1/1/2009')
    SET NOCOUNT OFF
    
    
    ;WITH YourTableCTE AS
    (SELECT
        dtlRenewalID, dtlSchemeID, dteEffectiveDate
            ,ROW_NUMBER() OVER(PARTITION by dtlSchemeID order by dtlSchemeID,dtlRenewalID) AS RowNumber
        FROM @YourTable
    )
    SELECT
        c.dtlRenewalID, c.dtlSchemeID, c.dteEffectiveDate, p.dteEffectiveDate AS dtePrevious, n.dteEffectiveDate AS dteNext
        FROM YourTableCTE                 c
            LEFT OUTER JOIN YourTableCTE  p ON c.dtlSchemeID=p.dtlSchemeID AND c.RowNumber-1=p.RowNumber
            LEFT OUTER JOIN YourTableCTE  n ON c.dtlSchemeID=n.dtlSchemeID AND c.RowNumber+1=n.RowNumber
    

    OUTPUT:

    dtlRenewalID dtlSchemeID dteEffectiveDate        dtePrevious             dteNext
    ------------ ----------- ----------------------- ----------------------- -----------------------
    1            1           2005-01-01 00:00:00.000 NULL                    2006-01-01 00:00:00.000
    2            1           2006-01-01 00:00:00.000 2005-01-01 00:00:00.000 2007-01-01 00:00:00.000
    3            1           2007-01-01 00:00:00.000 2006-01-01 00:00:00.000 2008-01-01 00:00:00.000
    4            1           2008-01-01 00:00:00.000 2007-01-01 00:00:00.000 2009-01-01 00:00:00.000
    5            1           2009-01-01 00:00:00.000 2008-01-01 00:00:00.000 NULL
    6            2           2005-01-01 00:00:00.000 NULL                    2006-01-01 00:00:00.000
    7            2           2006-01-01 00:00:00.000 2005-01-01 00:00:00.000 2007-01-01 00:00:00.000
    8            2           2007-01-01 00:00:00.000 2006-01-01 00:00:00.000 2008-01-01 00:00:00.000
    9            2           2008-01-01 00:00:00.000 2007-01-01 00:00:00.000 2009-01-01 00:00:00.000
    10           2           2009-01-01 00:00:00.000 2008-01-01 00:00:00.000 NULL
    
    (10 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server 2008 database that has two tables. These two tables
In SQL server 2008: Suppose I have two tables. Table1 has 3 fields: Name,
I have a SQL Server 2008 database. This database has two related tables: Customer
I have two tables, Table_A and Table_B. Table_A has a one-to-many relationship with Table_B.
I have SQL Server 2008 Ent and OLTP database with two big tables. How
I have two SQL Server 2008 databases called Anna and Bob . Bob has
I have two tables in a SQL Server 2008 database in my company. The
I am using SQL Server 2008, and I have two tables Table1 contains 3.5
I have two SQL Server 2008 Enterprise databases (on two machines), and one of
I have SQL Server 2008 database with two tables. The first table is called

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.