表格结构:
Category
CategoryID|CategoryCode|CategoryName
SubCategory
SubCategoryID|CategoryCode|SubCategoryCode|SubCategoryName
Products
ProductID|CategoryCode|SubCategoryCode|SKU|ProductName|UnitPrice
Orders
OrderID|OrderCode|CustomerCode|EmployeeCode|PurchaseDateTime
Order_Details
OrderDetailID|OrderCode|SKU|Quantity|Price|NetSales
实施例
CategoryCode - 'S'
SubCategoryCode - '08'
SKU - '003'
产品由S08003识别,依此类推
我希望从这个组合产品密钥中识别的5个最畅销产品中获得最高销量...请注意,order_details表中的SKU已经编译完毕。
答案 0 :(得分:0)
尝试子选择。
SELECT Order_Details.SKU,
SUM(Order_Details.Quantity * Order_Details.Price) AS Total_Revenue
FROM Order_Details
WHERE Order_Details.SKU IN
(SELECT X.SKU FROM Order_Details AS X
ORDER BY SUM(X.Quantity) DESC
GROUP BY X.SKU LIMIT 5)
GROUP BY Order_Details.SKU;