I have the following diagram, which I simplified a bit:
CREATE TABLE [dbo].[Header] ( [HeaderId] [int] IDENTITY(1,1) NOT NULL, [StaffId] [int] NOT NULL, [WeekEnding] [smalldatetime] NOT NULL, ... ) CREATE TABLE [dbo].[Staff] ( [StaffId] [int] NOT NULL, [FirstWeekEnding] [smalldatetime] NULL, ... )
StaffId in the header table is a foreign key.
The heading table tracks personnel-related data (not shown in the subcategory), and for that week there will be an entry for the "end of the week" if there is data in the auxiliary table. In this case, the "end of the week" is always Sunday. Therefore, sample data may look like this:
HeaderId StaffId WeekEnding --------------------------------- 1 1 13/02/2011 2 1 20/02/2011 etc...
The FirstWeekEnding value in the staff table is the first start date for tracking information in the header table.
My question
Given the end date of the first week of each employee, how do I build a query that will give me all the MISSING entries from the header table to the current date?
For example, given the following data:
StaffId FirstWeekEnding --------------------------- 1 02/01/2011 HeaderId StaffId WeekEnding --------------------------------- 1 1 02/01/2011 2 1 09/01/2011 3 1 16/01/2011 4 1 13/02/2011 5 1 20/02/2011
Result:
StaffId WeekEnding --------------------- 1 23/01/2011 1 30/01/2011 1 06/02/2011
Ideally, the request should be handled by several employees grouped by their StaffId.