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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:41:25+00:00 2026-06-18T02:41:25+00:00

In SQL Server 2008 R2, I have a table (StatDays) that contains a count

  • 0

In SQL Server 2008 R2, I have a table (StatDays) that contains a count of days (DaysInStatus) that an order was in a particular status. There is an entry for each day the order remained in that status, except for Sundays when there was no process to write to this table. In that case, the order would appear again on Monday, with DaysInStatus advanced two days.

My problem is how to populate the missing Sunday values. (I am assuming that if an order has a Saturday entry but no Monday entry, that Saturday is the last desired data point.)

Example:

OrderID StatDate    DaysInStatus
A11111  6/1/2012    20
A11111  6/2/2012    21
A11111  6/4/2012    23
A11111  6/5/2012    24
A11111  6/6/2012    25
A11111  6/7/2012    26
A11111  6/8/2012    27
A11111  6/9/2012    28
A11111  6/11/2012   30

Here I would want to insert (or select) into another table all of these values, plus the missing dates 6/3 and 6/10 – where DaysInStatus is advanced to 22 and 29, respectively – and the OrderID.

Also, an OrderID will NOT always appear in this table at DaysInStatus = 1. In can appear at almost any value, and drop out at almost any value.

I have tried generating a dates table, then doing left outer join to StatDays on statDate and StatDate+1, StatDate-1. This works for small recordsets but the actual table has over 100 million records, so this would not work for me.

Thanks in advance for any suggestions!

  • 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-06-18T02:41:26+00:00Added an answer on June 18, 2026 at 2:41 am

    Try this. It will help with any missing days.

    create table StatDays(OrderID varchar(20),StatDate datetime,
                          DaysInStatus int)   
    
    insert into statdays values('A11111',  '6/1/2012',    20)
    insert into statdays values('A11111',  '6/2/2012',    21)
    insert into statdays values('A11111',  '6/4/2012',    23)
    insert into statdays values('A11111',  '6/5/2012',    24)
    insert into statdays values('A11111',  '6/6/2012',    25)
    insert into statdays values('A11111',  '6/7/2012',    26)
    insert into statdays values('A11111',  '6/8/2012',    27)
    insert into statdays values('A11111',  '6/9/2012',    28)
    insert into statdays values('A11111',  '6/11/2012',   30)
    insert into statdays values('A11112',  '6/1/2012',    20)
    insert into statdays values('A11112',  '6/2/2012',    21)
    insert into statdays values('A11112',  '6/4/2012',    23)
    insert into statdays values('A11113',  '6/5/2012',    24)
    insert into statdays values('A11113',  '6/6/2012',    25)
    insert into statdays values('A11113',  '6/7/2012',    26)
    insert into statdays values('A11113',  '6/8/2012',    27)
    insert into statdays values('A11113',  '6/9/2012',    28)
    insert into statdays values('A11113',  '6/13/2012',   32)
    
    
    
    
    
    
    
    with 
    demo_cte as 
    (select orderid, min(statdate) Min_Date,max(statdate) Max_Date,
     min(DaysInStatus) Min_Day from StatDays group by orderid
    
     union all
    
    
     select orderid,DateAdd(day,1,Min_Date), Max_date,Min_day+1 from demo_cte
     where DateAdd(day,1,Min_Date)<=Max_date
    
    )
    select OrderId,Min_Date Stat_days,Min_Day  from demo_cte
    order by orderid
    

    SQL Fiddle

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

Sidebar

Related Questions

I'm using SQL Server 2008 and I have a table that has a 'Status'
I have a table in a sql server 2008 database that contains bunch of
I use SQL Server 2008 R2 and have a table that I want no
I have table designed in SQL-Server 2008 R2. I have a column in that
I have a table in SQL Server 2008 that looks kind of like this:
I have a table in my SQL Server 2008 database called dbo.app_additional_info which contains
System is: SQL Server 2008-R2 I have a table that one of its column
I have a table in sql server 2008, I need to identify the order
I'm using sql server 2005/2008 and I have a table that it's PK (rec_index)
Background: Entity Framework 4, with SQL Server 2008 Problem: I have a table Order

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.