- 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:

推薦閱讀
- 火格局的時空變異及其在電網防火中的應用
- Seven NoSQL Databases in a Week
- 計算機控制技術
- Effective DevOps with AWS
- MicroPython Projects
- Spark大數據技術與應用
- DevOps:Continuous Delivery,Integration,and Deployment with DevOps
- Hybrid Cloud for Architects
- 項目管理成功利器Project 2007全程解析
- OpenStack Cloud Computing Cookbook
- Apache源代碼全景分析(第1卷):體系結構與核心模塊
- 智慧未來
- AVR單片機工程師是怎樣煉成的
- 數字系統設計與Verilog HDL
- 小數據之美:精準捕捉未來的商業小趨勢