SQL:根据聚合值有条件地选择列

时间:2017-05-15 04:29:55

标签: sql-server conditional

这个问题的答案:

显示每年雇用的运营商总数。

就是这样:

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

但是我如何修改这个问题:

显示每年雇用多个运营商的运营商数量。

我试着做

WHERE Count >= 1

WHERE COUNT(YEAR(HireDate)) > 1

但没有任何效果。我做过研究CASE陈述,但是我找不到任何适合我需要做的事情,任何想法?

使用SQL Server 2008

1 个答案:

答案 0 :(得分:1)

使用Having子句

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

Read More