HAVING
更新時間:
HAVING
子句用于過濾GROUP BY
分組聚合后的數據。HAVING
子句必須與聚合函數和GROUP BY
子句一起使用,在分組和聚合計算完成后,再對分組進行過濾,篩選掉不滿足條件的分組。本文介紹HAVING
的用法和示例。
[ HAVING condition ]
注意事項
HAVING
條件中引用的列必須為分組列或引用了聚合函數結果的列。HAVING
子句必須與聚合函數以及GROUP BY
子句一起使用,用于對GROUP BY
分組進行過濾,去掉不滿足條件的分組。
示例
在CUSTOMER表中,進行分組查詢,查詢賬戶余額大于指定值的記錄。
SELECT count(*), mktsegment, nationkey,
CAST(sum(acctbal) AS bigint) AS totalbal
FROM customer
GROUP BY mktsegment, nationkey
HAVING sum(acctbal) > 5700000
ORDER BY totalbal DESC;
_col0 | mktsegment | nationkey | totalbal
-------+------------+-----------+----------
1272 | AUTOMOBILE | 19 | 5856939
1253 | FURNITURE | 14 | 5794887
1248 | FURNITURE | 9 | 5784628
1243 | FURNITURE | 12 | 5757371
1231 | HOUSEHOLD | 3 | 5753216
1251 | MACHINERY | 2 | 5719140
1247 | FURNITURE | 8 | 5701952
文檔內容是否對您有幫助?