我只是想仔细检查我写的这个SQL查询。我想根据订购的美元金额返回前五名客户名字?我正在使用一个名为“customer”的表和一个名为“orders”的表。但是,我不记得我是否需要在某处使用“max”....任何帮助都表示赞赏!
SELECT TOP 5
customer.customerFirstName
FROM customer
LEFT JOIN orders
ON customer.customerID = orders.customerID
ORDER BY orders.orderCost DESC
答案 0 :(得分:1)
我认为你需要group by
:
SELECT TOP 5 c.customerFirstName
FROM customer c LEFT JOIN
orders o
ON c.customerID = o.customerID
GROUP BY c.customerFirstName
ORDER BY SUM(o.orderCost) DESC;
答案 1 :(得分:0)
我认为这有助于回答您的问题,http://www.w3schools.com/sql/sql_join_left.asp
SELECT TOP 5 orders.orderid,
orders.customerid, customers.customername 来自客户 LEFT JOIN订单 ON customers.customerid = orders.customerid 按订单顺序排序DESC