如何根據銷售額選擇前 5 名供應商?
我有以下資料庫:

我嘗試了多種解決方案,這是最接近的解決方案,但我知道這是錯誤的,因為一個供應商不止一次出現。
SELECT TOP 5
(od.UnitPrice * (1 - od.Discount) * od.Quantity) total_sales,
od.Quantity, od.UnitPrice, od.Discount, S.ContactName
FROM
[Order Details] od
INNER JOIN
Products p ON p.ProductID = od.ProductID
INNER JOIN
Suppliers s ON s.SupplierID = p.SupplierID
ORDER BY
total_sales DESC

銷售額總和由以下公式提供:
SUM(UnitPrice * (1 - Discount) * Quantity)
任何幫助將不勝感激!
uj5u.com熱心網友回復:
您需要按供應商分組
SELECT TOP (5)
s.CompanyName,
s.ContactName,
SUM(od.UnitPrice * (1 - od.Discount) * od.Quantity) total_sales,
FROM
Suppliers s
INNER JOIN
Products p ON s.SupplierID = p.SupplierID
INNER JOIN
[Order Details] od ON p.ProductID = od.ProductID
GROUP BY
s.SupplierID,
s.CompanyName,
s.ContactName
ORDER BY
total_sales DESC;
Suppliers請注意,即使未選擇主鍵,它也是如何在分組中的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/413594.html
標籤:
上一篇:如何每月為每個id創建一行?
