Prefix: I am working on an application that is tracking employee training time and I need to get the total number of employees that have reached 75 total hours of training. After researching for some time I have determined I should probably use a join clause but joins are not my strength.
Question: For the following query I am having a problem getting the 'completed' subquery to return a 0 value.
SELECT
Completed.Total_Complete "Completed Training",
Incomplete.Total_Incomplete "Incompleted Training"
FROM
(
SELECT
count(*) AS Total_Complete
FROM
EMPLOYEE_TRAINING_RECORD
HAVING SUM(TRAINING_HOURS) >= 75
)
Completed,
(
SELECT
count(*) AS Total_Incomplete
FROM
EMPLOYEE_TRAINING_RECORD
HAVING SUM(TRAINING_HOURS) < 75
)
Incomplete
;
Thanks for any help.
Best Answer
You don't need a join. First you need to
GROUP BY
employee, in order to get the hours per employee. Assuming you have anemployee_id
that identifies employees:Then you need to do another
GROUP BY
, in the whole result set, to count the number of employees that have over and under 75 hours:You could also use window functions and not use subqueries but the code is rather obfuscated: