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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:44:11+00:00 2026-06-03T01:44:11+00:00

I have a table containing date , employeeID (int) , and ShiftWorked (can be

  • 0

I have a table containing date, employeeID(int) , and ShiftWorked (can be night/day/weekend or evening) . There is a row for each employee and date combination

I would like to construct a query that gives me a count of how many people have worked a night shift in the week before and after each date in the roster period.

--------------------------------------------------------------------------
Date (yyyy-MM-dd)     | CountOfNightshifts(for 1 week either side of date)
--------------------------------------------------------------------------

2012-1-1              |  8
2012-1-2              |  12
2012-1-3              |  11
2012-1-4              |  6 
etc                   |  etc

I hope this is clear. I have spent days trying to get this to work but I am not getting anywhere.

For example:

SELECT COUNT(id), [date]
FROM ROSTER
WHERE Shift = night AND [date] BETWEEN DATEADD(D,-7,[date]) AND DATEADD(d,7,[date])
GROUP by [date]
group by [date]

This will give me a list of dates and a count of nights on that particular day – not all night shifts in the 7 days before and after the date.

  • 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-03T01:44:12+00:00Added an answer on June 3, 2026 at 1:44 am

    The following query will return two columns: the reference (roster) date and the number of (distinct) people that have worked on the night sift seven days before to seven days after the reference date.

    SELECT tmain.date,
    (
        SELECT COUNT(DISTINCT taux.employeeId)
        FROM roster taux
        WHERE taux.shiftWorked = 'night'
          AND taux.date >= DATEADD(DAY, -7, tmain.date)
          AND taux.date <= DATEADD(DAY, 7, tmain.date)
    ) AS [number_of_distinct_people_with_night_shift]
    FROM roster tmain
    ORDER BY tmain.date;
    

    Note 1: Usually I prefer joins over sub-queries, but I guess this solution is easier to read.

    Note 2: I am assuming the time component of date values are irrelevant and all dates have the same time (i.e. ’00:00:00.00′); if it is not the case, there are more adjustments to be done on the date comparison.

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

Sidebar

Related Questions

I have a table containing records of the date and time each product was
I have this table: sportman { code int primary key, date Date. } containing
I have a table containing 2 date fields and an identifier (id, fromdate and
I have a table containing reports and the date/time they were created. I'd like
I have a table containing 60 million rows. The structure is like entryid, date,
I have a table with an JobID (PK), EmployeeID (FK), StartDate, EndDate containing data
I have a table in a Oracle database containing a date field EXPIRYDATE. I
I have a table in an Openoffice Database, containing two date columns. I'm trying
I have a table containing multiple records for different transactions i.e. ID Date REF
Let's say we have a [Valuations] table containing several values per date and per

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.