SQL: conditionally select column based on aggregate value

The answer to this question is:

Shows the total number of operators hired each year.

:

SELECT
    COUNT(YEAR(HireDate)) AS 'Count',
    YEAR(HireDate) AS 'Year'
FROM Operators
GROUP BY YEAR(HireDate);

      

But how do I change this for this question:

Display the number of operators employed in each year where more than one operator was employed.

I tried to do

WHERE Count >= 1

      

or

WHERE COUNT(YEAR(HireDate)) > 1

      

but nothing worked. I have been doing research on CASE statements but I couldn't find anything suitable for what I need, any idea?

Using SQL Server 2008

+3


source to share


1 answer


Use the sentence Having

SELECT
    COUNT(YEAR(HireDate)) AS 'Count',
    YEAR(HireDate) AS 'Year'
FROM Operators
GROUP BY YEAR(HireDate)
HAVING Count >= 1;

      



More details

+1


source







All Articles