这是我的第一个SQL查询实践,我想出了这个问题,我必须找到某个房屋的库存物品数量的平均值大于特定仓库的平均值。我得到的只是一个仓库列表及其平均值(不包括特定仓库),我只需保留仓库的平均值大于特定仓库的平均值。我该如何解决这个问题?这是我第一次学习SQL。谢谢
SELECT Warehouse, AVG(QuantityInStocks) AS Average
FROM SomeTable
WHERE Warehouse Not In ('Specific_Warehouse')
GROUP BY Warehouse;
答案 0 :(得分:2)
你应该在HAVING子句中使用子查询:
SELECT Warehouse, AVG(QuantityInStocks) AS Average
FROM SomeTable
WHERE Warehouse NOT IN ('Specific_Warehouse')
GROUP BY Warehouse
HAVING AVG(QuantityInStocks) >
(SELECT AVG(QuantityInStocks)
FROM SomeTable
WHERE Warehouse IN ('Specific_Warehouse')
GROUP BY Warehouse);
答案 1 :(得分:0)
关于你提到的问题
“我必须找到某处房屋库存的平均数量 这大于特定仓库的平均值“
您可以在NESTED SELECT
中使用A WHERE CLAUSE
:
SELECT AVG(QuantityInStocks) AS Average
FROM SomeTable
WHERE QuantityInStocks > (SELECT AVG(QuantityInStocks)
FROM SomeTable)