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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:56:48+00:00 2026-06-06T05:56:48+00:00

I am using SQL Server 2005 and am familiar with the basics, but have

  • 0

I am using SQL Server 2005 and am familiar with the basics, but have an issue that is a bit over my head which I would appreciate any help on.

I have two tables I am trying to work with. One is an employee database table and one is an organization table. In the employee table each employee has a department id associated with them and a current employment status. In the organization table there is a list of department ids, which department that department rolls up under, and a current active status. I need to create a query that I can give a department id to that will return the total number of active (value – A) employees in that department and all the active (value – A) departments below it all the way to the bottom.

The relevant tables and the columns:
EE_Persons_today
DEPTID
EMPL_STATUS

DEPARTMENT_DATA
DEPTID
REPORTS_TO_DEPT
EFF_STATUS

Example:
Department IT has 300 people directly assigned to it, but also has 2 subgroups, Software and Hardware. Hardware has 100 people directly assigned to it and no sub groups. Software has 100 people directly assigned to it and 1 subgroup called Enterprise with 50 people directly assigned to it. If I ask for how many are in IT it would be 300+100+100+50 = 550. If I ask for how many are in Software it would be 100+50=150.

  • 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-06T05:56:50+00:00Added an answer on June 6, 2026 at 5:56 am

    Check out CTEs (Common Table Expressions) for recursive SQL queries. I’ve include a sample below. Please let me know if it works for your setup.

    Change the value of @INPUT to the department for which you are requesting the head count (where EMPL_STATUS= ‘A’).

    DECLARE @INPUT INT
    SET @INPUT = 1
    
    
    DECLARE @DEPARTMENT_DATA TABLE
    (
        DEPTID INT,
        REPORTS_TO_DEPT INT,
        EFF_STATUS INT
    )
    
    
    INSERT INTO @DEPARTMENT_DATA
    SELECT 1, NULL, 1
    UNION SELECT 2, 1, 1
    UNION SELECT 3, 2, 1
    UNION SELECT 4, 1, 1
    UNION SELECT 5, 4, 1
    
    
    DECLARE @EE_Persons_today TABLE
    (
        ID INT IDENTITY(1,1),
        DEPTID INT,
        EMPL_STATUS VARCHAR(1)
    )
    
    INSERT INTO @EE_Persons_today
    SELECT 2, 'A'
    UNION ALL SELECT 2, 'A'
    UNION ALL SELECT 2, 'A'
    UNION ALL SELECT 2, 'A'
    UNION ALL SELECT 2, 'B'
    UNION ALL SELECT 3, 'A'
    UNION ALL SELECT 3, 'A'
    UNION ALL SELECT 3, 'B'
    UNION ALL SELECT 3, 'A'
    UNION ALL SELECT 3, 'A'
    UNION ALL SELECT 3, 'A'
    UNION ALL SELECT 3, 'A'
    UNION ALL SELECT 3, 'B'
    UNION ALL SELECT 3, 'A'
    UNION ALL SELECT 3, 'A'
    UNION ALL SELECT 4, 'B'
    UNION ALL SELECT 4, 'A'
    UNION ALL SELECT 5, 'A'
    UNION ALL SELECT 5, 'A'
    UNION ALL SELECT 5, 'B'
    UNION ALL SELECT 5, 'A'
    
    
    ; WITH CTE AS
    (
        SELECT a.DEPTID
        FROM @DEPARTMENT_DATA a
        WHERE a.DEPTID = @INPUT
    
            UNION ALL
    
        SELECT a.DEPTID
        FROM @DEPARTMENT_DATA a
        JOIN CTE c
            ON a.REPORTS_TO_DEPT = c.DEPTID
    ) 
    SELECT COUNT(*)
    FROM @EE_Persons_today a
    JOIN CTE c
        ON a.DEPTID = c.DEPTID
    WHERE a.EMPL_STATUS = 'A'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Sql Server 2005. I have a long running update that may take about
I am using SQL Server 2005. I have a site that people can vote
Using SQL Server 2005 I have two table which have the same datatype and
Using SQL Server 2005 developer edition on Windows XP pro (32-bit) I notice that
Using SQL Server 2005 I have a field that contains a datetime value. What
I'm using SQL Server (2005). Today I have some tables that everytime the data
Using SQL Server 2005 I have a query that gets child records from bundles
[using SQL Server 2005] I have a table full of users, I want to
I'm using SQL Server 2005 express I have a datetime field in a table
I am not too familiar with sql server 2005. I have a schema in

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.