1

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 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.