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

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:

主站蜘蛛池模板: 富宁县| 盐山县| 陈巴尔虎旗| 阿拉善盟| 定南县| 平湖市| 河东区| 尼玛县| 乌海市| 霍邱县| 滨海县| 铁岭县| 夏邑县| 青州市| 金华市| 滕州市| 兴仁县| 神池县| 安平县| 桃园市| 博乐市| 漾濞| 浙江省| 博湖县| 乐陵市| 昌宁县| 麦盖提县| 大姚县| 九江县| 焉耆| 灵寿县| 武冈市| 疏勒县| 平利县| 兰考县| 南澳县| 榆林市| 阳新县| 大邑县| 游戏| 梨树县|