如何选择最高价值

时间:2013-06-08 05:29:16

标签: sqlplus

SELECT B.CustomerID, SUM(C.UnitPrice * C.Quantity) AS "Total Value"
FROM Orders B, Order_Det C
WHERE B.OrderID = C.OrderID AND “Total Value” = (SELECT MAX(“Total Value”) FROM Order_Det)
GROUP BY B.CustomerID
ORDER BY "Total Value";

以上代码是我试过的。 能够制作MULTIPLE订单的客户。因此,我希望通过多个数量购买和单价的总和来显示最有价值的客户。 我遇到的问题是我无法存档谁是最有价值的客户。请指导我。 Tq

1 个答案:

答案 0 :(得分:0)

这里是!!

SELECT O.CustomerID, SUM(OD.Quantity*OD.UnitPrice) AS "Total Value"
FROM Orders O
INNER JOIN Order_Det OD ON O.OrderId = OD.OrderId
GROUP BY O.CustomerID
ORDER BY SUM(OD.Quantity*OD.UnitPrice)

SELECT O.CustomerID, SUM(OD.Quantity*OD.UnitPrice) AS "Total Value"
FROM Orders O, Order_Det OD 
WHERE O.OrderId = OD.OrderId
GROUP BY O.CustomerID
ORDER BY SUM(OD.Quantity*OD.UnitPrice)

对于大多数有价值的客户,

SELECT TOP 1 O.CustomerID
FROM Orders O 
INNER JOIN Order_Det OD ON O.OrderId = OD.OrderId
GROUP BY O.CustomerID
ORDER BY SUM(OD.Quantity*OD.UnitPrice) DESC