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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:55:36+00:00 2026-05-31T12:55:36+00:00

I have been creating a date range, but in some cases a have a

  • 0

I have been creating a date range, but in some cases a have a problem:

This is what I have: TABLE_1

date          customer_id      status      total
----         -------------    --------    -------
20120201         1                a         10
20120202         1                a         20
20120203         1                b         20
20120204         1                b         20
20120205         1                a         20
20120206         1                a         20
20120201         2                d         30
20120202         2                e         40

After the execution of my procedure, I have this: TABLE_2

customer_id      status      start_date       end_date
-------------    --------    -----------      ---------
     1              a         20120201        NULL
     1              b         20120203        20120131
     2              d         20120201        20120201
     2              e         20120202        NULL

But this is what i want, a table with date ranges based on customer_id and status (end_date represents register with most recent date): TABLE_3

customer_id      status      start_date       end_date
-------------    --------    -----------      ---------
     1              a         20120201        20120202
     1              b         20120203        20120204
     1              a         20120205        NULL
     2              d         20120201        20120201
     2              e         20120202        NULL

My store procedure look like this:

;WITH TEMP AS (
SELECT
    Date
    customer_id
    status
FROM table_1
GROUP BY
    date,
    customer_id,
    status
)
,TEMP2 AS (
  SELECT 
        ID  = ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY MAX(date) DESC),
        start_date  = MIN(date),
        end_date    = MAX(date),
        [customer_id],
        [status]
  FROM TEMP 
  GROUP BY 
        [customer_id],
        [status]
)
SELECT 
  A.customer_id,
  A.status,
  A.start_date,
  end_date      = DATEADD(DAY,-1,B.start_date)
FROM TEMP2 A
LEFT JOIN TEMP2 B
    ON A.customer_id = B.customer_id
    AND A.ID = B.ID + 1

I know my error is in the creation of CTE TEMP2, because this code can´t discriminate for a customer_id with a status with two occurrences in different ranges of time, based on the ‘group by’ sentence

I can´t figure out how to do that…

  • 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-31T12:55:37+00:00Added an answer on May 31, 2026 at 12:55 pm

    Try this. Hope it works now.

    DECLARE @table_1 TABLE ( 
        date DATETIME, 
        customer_id INT, 
        status CHAR(1), 
        total INT 
    ) 
    
    INSERT @table_1 (date, customer_id, status, total) 
    VALUES   
    ('20120201', 1, 'a', 10), 
    ('20120202', 1, 'a', 20),  
    ('20120203', 1, 'b', 20),  
    ('20120204', 1, 'b', 20),  
    ('20120205', 1, 'a', 20),  
    ('20120206', 1, 'a', 20),  
    ('20120201', 2, 'd', 30),  
    ('20120202', 2, 'e', 40) 
    
    
    
    ;WITH CTE_1 AS ( 
        SELECT  
            customer_id,  
            status, 
            date, 
            ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY date ASC) AS seq 
        FROM @table_1    
    ),
    CTE_2 AS (
        SELECT  
            customer_id,  
            status, 
            date, 
            seq,
            1 AS flg,
            1 AS seq2
        FROM CTE_1 
        WHERE 
            seq = 1
    
        UNION ALL 
    
        SELECT  
            CTE_1.customer_id,  
            CTE_1.status, 
            CTE_1.date, 
            CTE_1.seq,
            CASE WHEN CTE_2.status = CTE_1.status THEN 0 ELSE 1 END,
            CASE WHEN CTE_2.status = CTE_1.status THEN CTE_2.seq2 ELSE CTE_2.seq2 + 1 END
        FROM CTE_1   
        INNER JOIN CTE_2
            ON CTE_1.customer_id = CTE_2.customer_id
                AND CTE_1.seq = CTE_2.seq + 1   
    )
    SELECT 
        st.customer_id, 
        st.status, 
        st.date AS start_date, 
        DATEADD(DAY, -1, en.date) AS end_date  
    FROM CTE_2 AS st
    LEFT JOIN CTE_2 AS en
        ON st.customer_id = en.customer_id
            AND st.seq2 = en.seq2 - 1
            AND en.flg = 1
    WHERE
        st.flg = 1
    ORDER BY 
        st.customer_id,
        st.seq2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far I have been creating Web Portal but recently I had a request
There have been a few articles and questions about how to do this but
I could use some help on this problem. I'm creating an application using Symfony2
I have been assigned the task of creating some graphical stats for a website,
(I think this question has been asked before, but the answers have been contradictory
i have been having this issue for some time now, and have not gotten
I have been creating a website with Ruby on Rails, and will be hosting
I have been creating Unit tests like crazy and find that I'm often having
I have been creating a peer to peer connection for a new game, that
I have been creating an image gallery with jQuery, all is done. The images

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.