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

  • Home
  • SEARCH
  • 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 4080448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:55:54+00:00 2026-05-20T17:55:54+00:00

I have a table with data like so: ID StartDate EndDate EffectiveDate — ———

  • 0

I have a table with data like so:

ID    StartDate    EndDate     EffectiveDate
--    ---------    -------     -------------
1     1/1/2009     12/31/2009    12/31/2009
1     7/1/2009     12/31/2009    7/1/2009
1     8/1/2009     12/31/2009    8/1/2009
2     1/1/2010     12/31/2010    12/31/2010
2     3/1/2010     12/31/2010    12/31/2010

I need a query that will format it like this, however:

ID    StartDate    EndDate     EffectiveDate
--    ---------    -------     -------------
1     1/1/2009     6/30/2009    null
1     7/1/2009     7/31/2009    7/1/2009
1     8/1/2009     12/31/2009   8/1/2009
2     1/1/2010     2/28/2010    null
2     3/1/2010     12/31/2010   12/31/2010
...

It’s basically like a timeline with segmented “points”, and each point is a new StartDate with an EndDate of the next point, and so on.

I’ve tried using a CTE, as well as the following query:

   SELECT t1.RfqItemOptionId, 
          t1.StartDate, 
          MIN(t2.EffectiveDate) EndDate,  
          t1.EffectiveDate EffectiveDate
     FROM @OptionPeriods t1 
LEFT JOIN @OptionPeriods t2 ON t1.RfqItemOptionId = t2.RfqItemOptionId
                           AND t1.EffectiveDate < t2.EffectiveDate
 GROUP BY t1.RfqItemOptionId, t1.StartDate, t1.EffectiveDate

Which is close…but no cigar 🙁

Can anyone help?

  • 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-20T17:55:55+00:00Added an answer on May 20, 2026 at 5:55 pm
    ;With Base As
    (
        Select
              *
            , ROW_NUMBER() Over (Partition By ID Order By StartDate) RowNum
        From
            @OptionPeriods
    )
    Select
          B1.*
        , IsNull(B2.StartDate - 1, B1.EndDate) As NewEndDate
        , Case B1.RowNum When 1 then Null Else B1.StartDate End As NewEffectiveDate
    From
        Base B1
    Left Join
        Base B2
    On
        B1.ID = B2.ID
        AND
        B1.RowNum + 1 = B2.RowNum
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of data that looks a bit like this: Name StartTime
If i have a table of data like Option_id|Component_id|Option_parent|Option_name|Option_value 1 1 0 id 2
I have table cars: Id int, Model nvarchar(max), DateOfProduction (datetime). And data like: 1
i have table AuditLog with fields including: audited_id | audited_type That results in data
I have a report that I would like to base on a single SQL
I have a table like this: id | roll_no | name --------------------- 1 |
I have a table with data as following: id col1 col2 1 c1 1
I have a table with this schema: DeviceID int RoomID int DateInstalled datetime A
I am trying to modify a stored procedure I wrote to pull data from
I've got two tables, one holds reservations for a room, and the other is

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.