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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:12:48+00:00 2026-05-31T04:12:48+00:00

Suppose I have a table that looks like the following id | location |

  • 0

Suppose I have a table that looks like the following

id | location | dateHired | dateRehired | dateTerminated
1  | 1        | 10/1/2011 | NULL        | 12/1/2011
2  | 1        | 10/3/2011 | 11/1/2011   | 12/31/2011
3  | 5        | 10/5/2011 | NULL        | NULL
4  | 5        | 10/5/2011 | NULL        | NULL
5  | 7        | 11/5/2011 | NULL        | 12/1/2011
6  | 10       | 11/2/2011 | NULL        | NULL

and I wanted to condense that into a summary table such that:

location | date        | hires  | rehires |   terms
1        |  10/1/2011  |   1    |    0    |     0
1        |  10/3/2011  |   1    |    0    |     0
1        |  11/1/2011  |   0    |    1    |     0
1        |  12/1/2011  |   0    |    0    |     1
1        |  12/31/2011 |   1    |    0    |     0
5        |  10/5/2011  |   2    |    0    |     0

etc.

— what would that SQL look like? I was thinking it would be something to the effect of:

SELECT
  e.location
  , -- ?
  ,SUM(CASE WHEN e.dateHired IS NOT NULL THEN 1 ELSE 0 END) AS Hires
  ,SUM(CASE WHEN e.dateRehired IS NOT NULL THEN 1 ELSE 0 END) As Rehires
  ,SUM(CASE WHEN e.dateTerminated IS NOT NULL THEN 1 ELSE 0 END) As Terms
FROM
  Employment e
GROUP BY
  e.Location
  ,--?

But I’m not real keen if that’s entirely correct or not?

EDIT – This is for SQL 2008 R2.

Also,

INNER JOIN on the date columns assumes that there are values for all three categories, which is false; which is the original problem I was trying to solve. I was thinking something like COALESCE, but that doesn’t really make sense either.

  • 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-31T04:12:49+00:00Added an answer on May 31, 2026 at 4:12 am

    I am sure there is probably an easier, more elegant way to solve this. However, this is the simplest, quickest that I can think of this late that works.

    CREATE TABLE #Temp
    (
        Location INT,
        Date DATETIME,
        HireCount INT,
        RehireCount INT,
        DateTerminatedCount INT
    )
    
    --This will keep us from having to do an insert if does not already exist
    INSERT INTO #Temp (Location, Date)
    SELECT DISTINCT Location, DateHired FROM Employment
    UNION
    SELECT DISTINCT Location, DateRehired FROM Employment
    UNION
    SELECT DISTINCT Location, DateTerminated FROM Employment
    
    UPDATE #Temp
    SET HireCount = Hired.HireCount
    FROM #Temp
    JOIN
    (
        SELECT Location, DateHired AS Date, SUM(*) AS HireCount 
        FROM Employment
        GROUP BY Location, DateHired
    ) AS Hired
    
    UPDATE #Temp
    SET RehireCount= Rehire.RehireCount
    FROM #Temp
    JOIN
    (
        SELECT Location, DateRehired AS Date, SUM(*) AS RehireCount
        FROM Employment
        GROUP BY Location, DateRehired
    ) AS Rehire
        ON Rehire.Location = #Temp.Location AND Rehire.Date = #Temp.Date
    
    UPDATE #Temp
    SET DateTerminatedCount = Terminated.DateTerminatedCount
    FROM #Temp
    JOIN
    (
        SELECT Location, DateTerminated AS Date, SUM(*) AS DateTerminatedCount
        FROM Employment
        GROUP BY Location, DateTerminated
    ) AS Terminated
        ON Terminated.Location = #Temp.Location AND Terminated.Date = #Temp.Date
    
    SELECT * FROM #Temp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i have one table that holds Blogs. The schema looks like : ID
Suppose I have a MySQL table called MyTable, that looks like this: +----+------+-------+ |
Suppose I have a Person table that looks like this, where fk_FriendID is related
Suppose I have a simple MySQL table that looks like this: CREATE TABLE `my_table`
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
I have a table in MySQL that looks like this: studentID subjectID anotherID anotherID2
I have a table that looks like something like this: timestamp value person ===============================================
Suppose I have table A with a field that can be either 1 or
Suppose I have two columns in a table that represents a graph, the first
Suppose I have a table, the first column is an identity. I thought that

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.