I pieced together the below code from research from the net and my own SQL knowledge (not the greatest).
The table Table_One holds data for staff and their working days, what I am trying to do is INSERT rows where the dates are missing (non-working days) between two specified dates for each Staff member.
This is as far as I can get, I don't know how to check if the row is missing and if it is, insert the new row with the date and the corresponding staff members data.
SET NOCOUNT ON;
IF object_id('dbo.Tally') is not null drop table dbo.tally
GO
SELECT TOP 10000 IDENTITY(int,1,1) as ID
INTO dbo.Tally FROM master.dbo.SysColumns
ALTER table dbo.Tally
add constraint PK_ID primary key clustered(ID)
GO
select * from dbo.Tally
--Generate Date Range
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '2016/6/1'
SET @EndDate= '2016/7/1'
SELECT dateadd(DD,ID-1,@StartDate) as [DATE]
FROM dbo.Tally
WHERE dateadd(DD,ID-1,@StartDate)<=@EndDate
The table looks like this,
Staff_ID | Date | Year | Mon | Day | First_Name | Last_Name | Section | Time_Worked
1001 | 2016/6/1 | 2016 | 6 | 1 | Bill | Price | Level 1 | 2016/6/1 8:30:00.000
1001 | 2016/6/5 | 2016 | 6 | 1 | Bill | Price | Level 1 | 2016/6/5 8:30:00.000
1001 | 2016/6/9 | 2016 | 6 | 1 | Bill | Price | Level 1 | 2016/6/9 8:30:00.000
1001 | 2016/6/12 | 2016 | 6 | 1 | Bill | Price | Level 1 | 2016/6/12 8:30:00.000
1002 | 2016/6/1 | 2016 | 6 | 1 | Mary | Somers | Level 1 | 2016/6/1 8:30:00.000
1002 | 2016/6/5 | 2016 | 6 | 1 | Mary | Somers | Level 1 | 2016/6/5 8:30:00.000
1002 | 2016/6/8 | 2016 | 6 | 1 | Mary | Somers | Level 1 | 2016/6/8 8:30:00.000
1003 | 2016/6/3 | 2016 | 6 | 1 | Mark | Jones | Level 1 | 2016/6/3 8:30:00.000
1003 | 2016/6/5 | 2016 | 6 | 1 | Mark | Jones | Level 1 | 2016/6/5 8:30:00.000
The first row of data that falls in between each of the two dates for the staff member will be able to be used to fill the columns other than the date column. And each staff member first row of data will not necessarily be the same date.
Eg.
This staff members first day is two days after the SET @StartDate = '2016/6/1' in the query,
Staff_ID | Date | First_Name | Last_Name | Section | Time_Worked
1003 | 2016/6/3 | Mark | Jones | Level 1 | 2016/6/3 8:30:00.000
But , the other columns will be able to be used to fill the new rows data.
This is the outcome for one staff member from the table above, in this case ,staff No 1001 named Bill.
Staff_ID | Date | Year | Mon| Day| First_Name | Last_Name | Section | Time_Worked
1001 | 2016/6/1 | 2016 | 6 | 1 | Bill | Price | Level 1 | 2016/6/1 8:30:00.000
1001 | 2016/6/2 | 2016 | 6 | 2 | Bill | Price | Level 1 | NULL
1001 | 2016/6/3 | 2016 | 6 | 3 | Bill | Price | Level 1 | NULL
1001 | 2016/6/4 | 2016 | 6 | 4 | Bill | Price | Level 1 | NULL
1001 | 2016/6/5 | 2016 | 6 | 5 | Bill | Price | Level 1 | 2016/6/5 8:30:00.000
1001 | 2016/6/6 | 2016 | 6 | 6 | Bill | Price | Level 1 | NULL
1001 | 2016/6/7 | 2016 | 6 | 7 | Bill | Price | Level 1 | NULL
1001 | 2016/6/8 | 2016 | 6 | 8 | Bill | Price | Level 1 | NULL
1001 | 2016/6/9 | 2016 | 6 | 9 | Bill | Price | Level 1 | 2016/6/9 8:30:00.000
1001 | 2016/6/10 | 2016 | 6 | 10 | Bill | Price | Level 1 | NULL
1001 | 2016/6/11 | 2016 | 6 | 11 | Bill | Price | Level 1 | NULL
1001 | 2016/6/12 | 2016 | 6 | 12 | Bill | Price | Level 1 | 2016/6/12 8:30:00.000
1001 | 2016/6/13 | 2016 | 6 | 13 | Bill | Price | Level 1 | NULL
1001 | 2016/6/14 | 2016 | 6 | 14 | Bill | Price | Level 1 | NULL
1001 | 2016/6/15 | 2016 | 6 | 15 | Bill | Price | Level 1 | NULL
1001 | 2016/6/16 | 2016 | 6 | 16 | Bill | Price | Level 1 | NULL
1001 | 2016/6/17 | 2016 | 6 | 17 | Bill | Price | Level 1 | NULL
1001 | 2016/6/18 | 2016 | 6 | 18 | Bill | Price | Level 1 | NULL
1001 | 2016/6/19 | 2016 | 6 | 19 | Bill | Price | Level 1 | NULL
1001 | 2016/6/20 | 2016 | 6 | 20 | Bill | Price | Level 1 | NULL
1001 | 2016/6/21 | 2016 | 6 | 21 | Bill | Price | Level 1 | NULL
1001 | 2016/6/22 | 2016 | 6 | 22 | Bill | Price | Level 1 | NULL
1001 | 2016/6/23 | 2016 | 6 | 23 | Bill | Price | Level 1 | NULL
1001 | 2016/6/24 | 2016 | 6 | 24 | Bill | Price | Level 1 | NULL
1001 | 2016/6/25 | 2016 | 6 | 25 | Bill | Price | Level 1 | NULL
1001 | 2016/6/26 | 2016 | 6 | 26 | Bill | Price | Level 1 | NULL
1001 | 2016/6/27 | 2016 | 6 | 27 | Bill | Price | Level 1 | NULL
1001 | 2016/6/28 | 2016 | 6 | 28 | Bill | Price | Level 1 | NULL
1001 | 2016/6/29 | 2016 | 6 | 29 | Bill | Price | Level 1 | NULL
1001 | 2016/6/30 | 2016 | 6 | 30 | Bill | Price | Level 1 | NULL
I have a While Loop that is working for me and updating the missing records for now, but the performance is terrible.
Thanks
while loopthat is currently updating the database with missing dates and data. But I have to loop through each staff member for each day in the month and with over 10,000 records it is taking too long.