官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 资兴市| 陇南市| 荆门市| 尤溪县| 太和县| 康马县| 搜索| 安福县| 临高县| 疏附县| 如东县| 安丘市| 庆云县| 宜章县| 铅山县| 泰州市| 兴安盟| 兴安盟| 融水| 华容县| 昭平县| 乐平市| 通榆县| 岢岚县| 托里县| 镇平县| 博野县| 大姚县| 上饶县| 阿拉善右旗| 黄浦区| 吉隆县| 全南县| 中卫市| 南汇区| 合山市| 扎兰屯市| 尼木县| 宁晋县| 济源市| 北川|