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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:43:18+00:00 2026-05-20T05:43:18+00:00

I have the following schema, which I’ve simplified slightly: CREATE TABLE [dbo].[Header] ( [HeaderId]

  • 0

I have the following schema, which I’ve simplified slightly:

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 on the Header table is a foreign key.

The Header table tracks data related to staff (in a sub table not shown) and for a given week there will be an entry for the “week end” if data in the sub table exists. In this case the “week end” is always a Sunday. Therefore, sample data could look like this:

HeaderId    StaffId    WeekEnding
---------------------------------
1           1          13/02/2011
2           1          20/02/2011
etc...

The “FirstWeekEnding” value on the staff table is the first date they started tracking information in the Header table.

My Question

Given the first week ending date of each staff member, how do I construct a query that will give me all the MISSING records from the Header table up to the present 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

The result would be:

StaffId    WeekEnding
---------------------
1          23/01/2011
1          30/01/2011
1          06/02/2011

Ideally the query should handle multiple staff members, grouped by their StaffId.

  • 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-20T05:43:19+00:00Added an answer on May 20, 2026 at 5:43 am

    Assuming you’re using SQL 2005+ you could generate all the weeks for each staff and attempt to join against the Header table in a CTE (something like, but perhaps not exactly this):

    ;WITH cte AS 
    ( SELECT StaffId, FirstWeekEnding AS WeekEnding
      FROM STAFF
      UNION ALL
      SELECT StaffId, DATEADD(D, 7, WeekEnding) FROM cte
      WHERE DATEADD(D, 7, WeekEnding) <= GETDATE()
    )
    SELECT StaffId, WeekEnding
    FROM cte LEFT JOIN Header ON cte.StaffId = Header.StaffId AND cte.WeekEnding = Header.WeekEnding
    WHERE Header.WeekEnding IS NULL
    OPTION (MAXRECURSION 32767)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
In a table I have the following schema table1: playerID int primary key, nationalities
I have the following table schema which maps user_customers to permissions on a live
I have a pretty simple table in SQLite, with the following schema: CREATE TABLE
I have a database where I store objects. I have the following (simplified) schema
I have the following schema, which I use to ensure that a person's PhoneNumber
I have the following XML part which schema I can't change. NUMBER, REGION, MENTION,
I have the following mysql table schema: SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; -- -- Database: `network` --
I am working on a appliaction in which I have a following schema Tasks
I have the following DB Schema :- Data is ... Location Table 1. New

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.