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

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:

主站蜘蛛池模板: 弋阳县| 桂林市| 桐柏县| 手游| 镇安县| 比如县| 班玛县| 大方县| 枣庄市| 邯郸市| 惠东县| 阿鲁科尔沁旗| 马鞍山市| 绥滨县| 合阳县| 星子县| 奎屯市| 永泰县| 巴东县| 筠连县| 名山县| 安泽县| 蒙阴县| 惠来县| 突泉县| 潍坊市| 宜昌市| 田林县| 肃北| 大余县| 綦江县| 兴仁县| 新和县| 山东| 娄烦县| 永州市| 郸城县| 垫江县| 咸丰县| 通河县| 宁陵县|