显示 SQL 中售出的最少单位的产品
Display product with minimum units sold in SQL
我的任务是设计以下查询:
"Out of the products that sold units in 2019 (that is, that sold at least one unit), we want to know the product that sold the least number of units. Your table must show:
- 产品编号
- 产品名称
- 商品分类名称"
我附上了数据库的截图。
事实上,我能够检索与最小销售单位对应的值 [在本例中为 95],但也无法在 MS Access 中显示与该值对应的产品名称。
使用以下代码显示正确的最小值:
SELECT MIN(UnitsSold)
FROM
(SELECT Products.ProductID, ProductName, Categories.CategoryID, CategoryName, SUM(Quantity) AS UnitsSold
FROM Orders, OrderDetails, Products, Categories
WHERE Orders.OrderID = OrderDetails.OrderID AND OrderDetails.ProductID = Products.ProductID And Products.CategoryID = Categories.CategoryID AND OrderDate BETWEEN #01/01/2019# AND #12/31/2019#
GROUP BY Products.ProductID, ProductName, Categories.CategoryID, CategoryName
HAVING SUM(Quantity) >= 1);
但是当我尝试通过此修改也显示该产品时:
SELECT MIN(UnitsSold, Products.ProductID, ProductName
FROM
(SELECT Products.ProductID, ProductName, Categories.CategoryID, CategoryName, SUM(Quantity) AS UnitsSold
FROM Orders, OrderDetails, Products, Categories
WHERE Orders.OrderID = OrderDetails.OrderID AND OrderDetails.ProductID = Products.ProductID And Products.CategoryID = Categories.CategoryID AND OrderDate BETWEEN #01/01/2019# AND #12/31/2019#
GROUP BY Products.ProductID, ProductName, Categories.CategoryID, CategoryName
HAVING SUM(Quantity) >= 1);
我遇到错误:
Your query does not include the specified expression 'ProductID' as part of an aggregate function
为什么不直接使用 ORDER BY
和 TOP 1
?
SELECT TOP (1)
p.ProductID,
ProductName,
c.CategoryName,
FROM
Orders o
INNER JOIN OrderDetails od ON o.OrderID = od.OrderID
INNER JOIN Products p ON od.ProductID = p.ProductID
INNER JOIN Categories c ON p.CategoryID = c.CategoryID
WHERE OrderDate BETWEEN #01/01/2019# AND #12/31/2019#
GROUP BY
p.ProductID,
ProductName,
c.CategoryName
HAVING SUM(Quantity) >= 1
ORDER BY SUM(Quantity) DESC
备注:
始终使用显式连接(使用 ON
关键字)而不是隐式连接(在 FROM
子句中使用逗号),其语法更难理解并且已经下降20多年前失宠
table 别名使查询更易于编写和阅读;此外,在多 table 查询中,您希望限定所有列名(查询中缺少一些 table 前缀)。
我的任务是设计以下查询:
"Out of the products that sold units in 2019 (that is, that sold at least one unit), we want to know the product that sold the least number of units. Your table must show:
- 产品编号
- 产品名称
- 商品分类名称"
我附上了数据库的截图。
事实上,我能够检索与最小销售单位对应的值 [在本例中为 95],但也无法在 MS Access 中显示与该值对应的产品名称。
使用以下代码显示正确的最小值:
SELECT MIN(UnitsSold)
FROM
(SELECT Products.ProductID, ProductName, Categories.CategoryID, CategoryName, SUM(Quantity) AS UnitsSold
FROM Orders, OrderDetails, Products, Categories
WHERE Orders.OrderID = OrderDetails.OrderID AND OrderDetails.ProductID = Products.ProductID And Products.CategoryID = Categories.CategoryID AND OrderDate BETWEEN #01/01/2019# AND #12/31/2019#
GROUP BY Products.ProductID, ProductName, Categories.CategoryID, CategoryName
HAVING SUM(Quantity) >= 1);
但是当我尝试通过此修改也显示该产品时:
SELECT MIN(UnitsSold, Products.ProductID, ProductName
FROM
(SELECT Products.ProductID, ProductName, Categories.CategoryID, CategoryName, SUM(Quantity) AS UnitsSold
FROM Orders, OrderDetails, Products, Categories
WHERE Orders.OrderID = OrderDetails.OrderID AND OrderDetails.ProductID = Products.ProductID And Products.CategoryID = Categories.CategoryID AND OrderDate BETWEEN #01/01/2019# AND #12/31/2019#
GROUP BY Products.ProductID, ProductName, Categories.CategoryID, CategoryName
HAVING SUM(Quantity) >= 1);
我遇到错误:
Your query does not include the specified expression 'ProductID' as part of an aggregate function
为什么不直接使用 ORDER BY
和 TOP 1
?
SELECT TOP (1)
p.ProductID,
ProductName,
c.CategoryName,
FROM
Orders o
INNER JOIN OrderDetails od ON o.OrderID = od.OrderID
INNER JOIN Products p ON od.ProductID = p.ProductID
INNER JOIN Categories c ON p.CategoryID = c.CategoryID
WHERE OrderDate BETWEEN #01/01/2019# AND #12/31/2019#
GROUP BY
p.ProductID,
ProductName,
c.CategoryName
HAVING SUM(Quantity) >= 1
ORDER BY SUM(Quantity) DESC
备注:
始终使用显式连接(使用
ON
关键字)而不是隐式连接(在FROM
子句中使用逗号),其语法更难理解并且已经下降20多年前失宠table 别名使查询更易于编写和阅读;此外,在多 table 查询中,您希望限定所有列名(查询中缺少一些 table 前缀)。