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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:59:32+00:00 2026-05-24T07:59:32+00:00

Net hires for a date range I have a set of employee data with

  • 0

Net hires for a date range

I have a set of employee data with these fields. termination date is null if they’re still active.

id   |    hire date    | termination date
1           7/25/11     |         null
2           12/01/10    |  7/30/11
3           7/20        |  null
4           7/16        | 7/29/11

What I need to get is for a rolling 13 week period is:

week ending date     Net hires
07-17                   1
07-24                   1
07-31          -1

…

I’ve begun doing it with a CTE and then using unions because I gave up trying to figure it out the better way. I have to do this for some other fields too so maybe there’s a better way, maybe using pivot/unpivot?

Rough pseudocode of what I have now.

;with foo
as
(
select *
from employee
--joins and conditions
)
select @report_date, sum(case when hire date > report_date-7 and hire_date < report_date then 1 else 0 end)(

... etc

I’ll take care of dates with no hires/terminations later.

  • 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-24T07:59:34+00:00Added an answer on May 24, 2026 at 7:59 am

    The fact that you are putting all of these UNIONs together with a hard-coded column should tell you that you’re missing a set somewhere. In this case it’s the set of weeks. Put those in a table (permanent, temporary, or virtual via a CTE) and this query becomes trivial.

    SELECT
        WKS.start_date,
        COUNT(E.employee_id)
    FROM
        Report_Weeks WKS
    LEFT OUTER JOIN Employees E ON
        E.hire_date > WKS.start_date AND
        E.hire_date < WKS.end_date
    GROUP BY
        WKS.start_date
    ORDER BY
        WKS.start_date
    

    Since you refer to “net” hires I’m guessing that you want to work in the terminations as well. You can do that by turning it into a SUM(CASE...) where you use 1 for new hires, 0 for people outside of that week or who were hired then fired within the same week, and -1 for terminations.

    As JNK points out, order matters in the CASE statement. Also, you can use the ELSE to handle the 0 values. I would suggest something like this:

    CASE
        WHEN
            E.hire_date BETWEEN WKS.start_date AND WKS.end_date AND
            E.termination_date > WKS.end_date THEN 1
        WHEN
            E.termination_date BETWEEN WKS.start_date AND WKS.end_date AND
            E.hire_date < WKS.end_date THEN -1
        ELSE 0
    END
    

    Since I used BETWEEN there you need to make sure that your start date for one week isn’t exactly the same as the end date for the previous week.

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

Sidebar

Related Questions

.NET I want to clone a value type's fields. How can i set a
I have an ASP.NET text control FromDate whose visible property is set to false,
.NET we have primitive datatypes like int and value types like struct. And also
I have an asp.net panel with a style which hides it and I'm using
I have some asp.net site that shows png images that converted from hi-res tiff
I have a asp.net component inside a ColorBox window, and after choosing a country
I need to enable the validation of hidden fields using ASP.net MVC3 unobtrusive validation.
I have written a .NET Windows service which has a WCF service built into
i am using .net 2.0. I have 2 images both relate to one a
I have the code here: http://jsfiddle.net/vFJvL/ when you mouseover Submenu3 the product list dropsdown

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.