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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:48:09+00:00 2026-05-27T13:48:09+00:00

I have the following table: row_num customer_status effective_from_datetime ——- —————— ———————– 1 Active 2011-01-01

  • 0

I have the following table:

row_num customer_status    effective_from_datetime
------- ------------------ -----------------------
1       Active             2011-01-01
2       Active             2011-01-02
3       Active             2011-01-03
4       Suspended          2011-01-04
5       Suspended          2011-01-05
6       Active             2011-01-06

And am trying to achieve the following result whereby consecutive rows with the same status are merged into one row with an effective from and to date range:

customer_status effective_from_datetime effective_to_datetime
--------------- ----------------------- ---------------------
Active          2011-01-01              2011-01-04
Suspended       2011-01-04              2011-01-06
Active          2011-01-06              NULL

I can get a recursive CTE to output the correct effective_to_datetime based on the next row, but am having trouble merging the ranges.

Code to generate sample data:

CREATE TABLE #temp
(
row_num INT IDENTITY(1,1),
customer_status VARCHAR(10),
effective_from_datetime DATE
)

INSERT INTO #temp
VALUES 
('Active','2011-01-01')
,('Active','2011-01-02')
,('Active','2011-01-03')
,('Suspended','2011-01-04')
,('Suspended','2011-01-05')
,('Active','2011-01-06')
  • 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-27T13:48:10+00:00Added an answer on May 27, 2026 at 1:48 pm

    EDIT SQL updated as per comment.

    WITH
      group_assigned_data AS
    (
      SELECT
        ROW_NUMBER() OVER (PARTITION BY customer_status ORDER BY effective_from_date) AS status_sequence_id,
        ROW_NUMBER() OVER (                             ORDER BY effective_from_date) AS sequence_id,
        customer_status,
        effective_from_date
      FROM
        your_table
    )
    ,
      grouped_data AS
    (
      SELECT
        customer_status,
        MIN(effective_from_date)   AS min_effective_from_date,
        MAX(effective_from_date)   AS max_effective_from_date
      FROM
        group_assigned_data
      GROUP BY
        customer_status,
        sequence_id - status_sequence_id
    )
    SELECT
      [current].customer_status,
      [current].min_effective_from_date       AS effective_from,
      [next].min_effective_from_date          AS effective_to
    FROM
      grouped_data   AS [current]
    LEFT JOIN
      grouped_data   AS [next]
        ON [current].max_effective_from_date = [next].min_effective_from_date + 1
    ORDER BY
      [current].min_effective_from_date
    

    This isn’t recursive, but that’s possibly a good thing.

    It doesn’t deal with gaps in your data. To deal with that you could create a calendar table, with every relevant date, and join on that to fill missing dates with ‘unknown’ status, and then run the query against that. (Infact you cate do it it a CTE that is used by the CTE above).

    At present…
    – If row 2 was missing, it would not change the result
    – If row 3 was missing, the end_date of the first row would change

    Different behaviour can be determined by preparing your data, or other methods. We’d need to know the business logic you need though.

    If any one date can have multiple status entries, you need to define what logic you want it to follow. At present the behaviour is undefined, but you could correct that as simply as adding customer_status to the ORDER BY portions of ROW_NUMBER().

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

Sidebar

Related Questions

I have the following table in MSSQL2005 id | business_key | result 1 |
I currently have the following row in my table: course_data: user_id days <-- This
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have the following table relationship in my database: Parent / \ Child1 Child2
I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following table in MySQL (version 5): id int(10) UNSIGNED No auto_increment
I have the following table custid ordid qty datesold 1 A2 12 2008-01-05 2
I have the following table: 'committee' table commname profname ======================== commA bill commA jack
I have the following table and data in SQL Server 2005: create table LogEntries

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.