OkPlankton
Well-Known Member
Hey SQL Guru's 
I'm stuck on something that may be very simple but can't wrap my head around it.
I have a few columns with a GROUP BY and a simple join. I also have one aggregration column; [No of Employees]
Results:

Now I need to add an additional aggregrate column; [No of ACTIVE employees]. So I did this with a CASE Statement and the [TERM_DT]; If the termination date is null then the employee is active.
This gave me an error because I have to either enclose the TERM_DT in an aggregate or add it in the GROUP BY:

I can't enclose the TERM_DT in an aggregate because it will break the logic of finding "Active Employees" So I added the TERM_DT in the GROUP BY
I get almost what I want. It show's the active employees but now it's grouped by date, I only want to group by the initial four groups; Site, Team, Gender, Race - NB: I know I can remove it from the select but it will still be grouped by date. If we look at rows 1 and 2, It's supposed to be; 3 Females with Race A in Team Jimson Bill in one row.

Any help or some resources would be really appreciated!
I'm stuck on something that may be very simple but can't wrap my head around it.
I have a few columns with a GROUP BY and a simple join. I also have one aggregration column; [No of Employees]
SELECT
H.CALL_CENTER AS [Site],
H.TEAM_LEAD_NAME AS [Team],
ED.GENDER AS [Gender],
ED.RACE AS [Race],
COUNT(ED.AGENT_ID) AS [No Of Employees]
FROM [dbo].[Hierarchy] H
JOIN [dbo].[EmployeeDetails] ED ON H.AGENT_ID = ED.AGENT_ID
GROUP BY
H.CALL_CENTER,
H.TEAM_LEAD_NAME,
ED.GENDER,
ED.RACE
Results:

Now I need to add an additional aggregrate column; [No of ACTIVE employees]. So I did this with a CASE Statement and the [TERM_DT]; If the termination date is null then the employee is active.
SELECT
H.CALL_CENTER AS [Site],
H.TEAM_LEAD_NAME AS [Team],
ED.GENDER AS [Gender],
ED.RACE AS [Race],
COUNT(ED.AGENT_ID) AS [No Of Employees],
[B]CASE WHEN H.TERM_DT IS NULL THEN COUNT(ED.AGENT_ID) ELSE '' END [/B]AS [No Of ACTIVE Employees]
FROM [dbo].[Hierarchy] H
JOIN [dbo].[EmployeeDetails] ED ON H.AGENT_ID = ED.AGENT_ID
GROUP BY
H.CALL_CENTER,
H.TEAM_LEAD_NAME,
ED.GENDER,
ED.RACE
This gave me an error because I have to either enclose the TERM_DT in an aggregate or add it in the GROUP BY:

I can't enclose the TERM_DT in an aggregate because it will break the logic of finding "Active Employees" So I added the TERM_DT in the GROUP BY
SELECT
H.CALL_CENTER AS [Site],
H.TEAM_LEAD_NAME AS [Team],
ED.GENDER AS [Gender],
ED.RACE AS [Race],
H.TERM_DT AS [TERMINATION DATE],
COUNT(ED.AGENT_ID) AS [No Of Employees],
CASE WHEN DATEADD(YEAR,3, H.TERM_DT) IS NULL THEN COUNT(ED.AGENT_ID) ELSE '' END AS [No Of ACTIVE Employees]
FROM [dbo].[Hierarchy] H
JOIN [dbo].[EmployeeDetails] ED ON H.AGENT_ID = ED.AGENT_ID
GROUP BY
H.CALL_CENTER,
H.TEAM_LEAD_NAME,
ED.GENDER,
ED.RACE,
[B]H.TERM_DT[/B]
I get almost what I want. It show's the active employees but now it's grouped by date, I only want to group by the initial four groups; Site, Team, Gender, Race - NB: I know I can remove it from the select but it will still be grouped by date. If we look at rows 1 and 2, It's supposed to be; 3 Females with Race A in Team Jimson Bill in one row.

Any help or some resources would be really appreciated!
