1

我正在使用以下 SQL 语句:

SELECT Col1, count(*) as Col2, Col3
FROM table1
WHERE WeekEnding = '08/26/2012' 
GROUP BY Col1

要得到

Col1   Col2  Col3
 NY     2     2
 FL     1     2
 TX     1     2
 NJ     1     2
 WA     2     2
 DE     1     3
 MA     4     4
 AL     3     3

如何排除 COL2 = COL3 的记录,所以我得到了。

Col1   Col2  Col3
 FL     1     2
 TX     1     2
 NJ     1     2
 DE     1     3
4

2 回答 2

2

将其添加到您的HAVING条款中

SELECT Col1, count(*) as Col2, Col3
FROM table1
WHERE WeekEnding = '08/26/2012' 
GROUP BY Col1
HAVING (count(*) <> col3)
于 2012-08-24T00:43:02.580 回答
2

您添加HAVING-完成havinggroup工作。Wheregroup.

SELECT Col1, count(*) as Col2, Col3
FROM table1
WHERE WeekEnding = '08/26/2012' 
GROUP BY Col1
HAVING Col2 != Col3

注意:您按 Col1 分组,但不是 Col3 - 这是不合法的,因为您在输出中请求 Col3。

于 2012-08-24T00:43:58.990 回答