- Learn T-SQL Querying
- Pedro Lopes Pam Lahoud
- 160字
- 2021-06-24 14:38:10
HAVING
HAVING further filters the result based on values in the results, rather than the actual data. A HAVING clause only applies to columns that are included in the GROUP BY clause or in an aggregate function. Building on the same example used in the WHERE, ORDER BY, and GROUP BY sections, here we want to additionally know which locations carry an inventory of over 100 items per product. For that, after the GROUP BY clause, the query has a HAVING clause over the aggregate function, where its result is greater than 100:
SELECT P.Name AS ProductName, SUM([PI].Quantity) AS Total_Quantity, L.Name AS LocationName
FROM Production.Product AS P
INNER JOIN Production.ProductInventory AS [PI] ON P.ProductID = [PI].ProductID
INNER JOIN Production.Location AS L ON [PI].LocationID = L.LocationID
WHERE P.Name LIKE 'Touring%'
GROUP BY P.Name, L.Name
HAVING SUM([PI].Quantity) > 100
ORDER BY P.Name DESC, L.Name DESC;
The following screenshot shows the results as containing only rows with an aggregate Total_Quantity greater than 100:

推薦閱讀
- 傳感器技術應用
- WordPress Theme Development Beginner's Guide(Third Edition)
- 21天學通Visual C++
- Implementing Splunk 7(Third Edition)
- Enterprise PowerShell Scripting Bootcamp
- 云原生架構進階實戰(zhàn)
- 基于ARM9的小型機器人制作
- Mastering Text Mining with R
- Mastering Ceph
- JRuby語言實戰(zhàn)技術
- Learn Microsoft Azure
- Effective Business Intelligence with QuickSight
- 精通ROS機器人編程(原書第2版)
- 菜鳥起飛五筆打字高手
- NetSuite ERP for Administrators