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!
Try this. It will help with any missing days.
SQL Fiddle