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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:49:39+00:00 2026-05-13T13:49:39+00:00

Given this table: How can I get the datediff in days between each status_date

  • 0

Given this table:

alt text

How can I get the datediff in days between each status_date for each group of ID_Number? In other words I need to find the number of elapsed days for each status that the ID_Number has been given.

Some things to know:

  • All ID_Number will have a received_date which should be the earliest date for each ID_Number (but app doesn’t enforce)
  • For each ID_Number there will be a status with a corresponding status_date which is the date that the ID_Number was given that particular status.
  • The status column doesn’t always necessarily go in the same order every time (app doesn’t enforce)
  • All ID_Number will have a closed_date which should be the latest date (but app doesn’t enforce)

Sample output:
So for ID_Number 2001, the first date (received_date) is 2009-05-02 and the next date you encounter has a status of ‘open’ and is 2009-05-02 so elapsed days is 0. Moving on to the next date encountered is 2009-05-10 with a status of ‘invest’ and the elapsed days is 8 counting from the prior date. The next date encountered is 2009-07-11 and the elapsed days is 62 counting from the previous date.

Edited to add:

Is it possible to have the elapsed days end up as a column on this table/view?
I also forgot to add that this is SQL Server 2000.

  • 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-13T13:49:40+00:00Added an answer on May 13, 2026 at 1:49 pm

    What I understand is that you need the difference between the first status_date and the next status_date for the same id and so on up to the closed_date.

    This will only work in SQL 2005 and up.

    ;with test as (
        select 
            key,
            id_number,
            status,
            received_date,
            status_date,
            closed_date,
            row_number() over (partition by id order by status_date, key ) as rownum
        from @test
        )
    select 
        t1.key,
        t1.id_number,
        t1.status,
        t1.status_date,
        t1.received_date,
        t1.closed_date,
        datediff(d, case when t1.rownum = 1 
                    then t1.received_date
                    else    
                        case when t2.status_date is null 
                            then t1.closed_date 
                            else t2.status_date 
                        end
                end,
                t1.status_date
             ) as days
    from test t1
    left outer join test t2
    on t1.id = t2.id
        and t2.rownum = t1.rownum - 1
    

    This solution will work with SQL 2000 but I am not sure how good will perform:

    select *,
        datediff(d,
            case when prev_date is null
                then closed_date
                else prev_date
            end,
            status_date )
    from ( 
        select *,
            isnull( ( select top 1 t2.status_date 
              from @test t2
              where t1.id_number = t2.id_number
                and t2.status_date < t1.status_date
              order by t2.status_date desc
              ),received_date) as prev_date 
        from @test t1
    ) a
    order by id_number, status_date
    

    Note: Replace the @Test table with the name of your table.

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

Sidebar

Related Questions

Hoping I can get some architectural advice on this. I have been given the
Given this table design in a web application: CREATE TABLE `invoice` ( `invoice_nr` int(10)
given this table definition create table herb.app (appId int identity primary key , application
Given this HTML: <div id=TABLE1 class=tabs> <table> <tbody datasrc=Music> <tr id=randomid>lorem</tr> <tr id=foo>lorem</tr> <tr
I hope that in this post, I can get people's opinions on best practices
Given this table: Order custName description to_char(price) A desa $14 B desb $14 C
I am newbie to db programming and need help with optimizing this query: Given
I'm trying to delete the highest value from a group. Given this data: group_table
For example, given this table of sparse ids: |id| | 1| | 2| |
I need to respond to a hover over a table cell. This css code

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.