我对简单的SQL查询有疑问。
这是我在Access 2013中的查询:
SELECT Shippers.Shipper_ID, Shippers.ShipperName, SUM(Orders.TotalPrice) AS TotPr FROM Orders
LEFT JOIN Shippers
ON Orders.Shipper_ID=Shippers.Shipper_ID
GROUP BY Shippers.Shipper_ID, Shippers.ShipperName
ORDER BY Shippers.ShipperName;
......它完美无缺。但是如何将别名列(TotPr)放入ORDER BY?
感谢您的帮助:)
答案 0 :(得分:0)
您可以使用以下公式:
SELECT Shippers.Shipper_ID, Shippers.ShipperName, SUM(Orders.TotalPrice) AS TotPr
FROM Orders LEFT JOIN
Shippers
ON Orders.Shipper_ID=Shippers.Shipper_ID
GROUP BY Shippers.Shipper_ID, Shippers.ShipperName
ORDER BY Shippers.ShipperName, SUM(Orders.TotalPrice);
答案 1 :(得分:0)
使用SUM(Orders.TotalPrice)
代替别名,您需要汇总
所以你的查询将是:
SELECT Shippers.Shipper_ID, Shippers.ShipperName, SUM(Orders.TotalPrice) AS TotPr
FROM Orders LEFT JOIN
Shippers
ON Orders.Shipper_ID=Shippers.Shipper_ID
GROUP BY Shippers.Shipper_ID, Shippers.ShipperName
ORDER BY Shippers.ShipperName, SUM(Orders.TotalPrice);
答案 2 :(得分:0)
Order By通常不喜欢计算列,因为大多数引擎都在内部计算SQL。试试这个:
SELECT Shipper_ID, ShipperName, TotPr
FROM
(SELECT Shippers.Shipper_ID, Shippers.ShipperName, SUM(Orders.TotalPrice) AS TotPr FROM Orders
LEFT JOIN Shippers
ON Orders.Shipper_ID=Shippers.Shipper_ID
GROUP BY Shippers.Shipper_ID, Shippers.ShipperName) ShipQuery
ORDER BY ShipQuery.ShipperName, ShipQuery.TotPr;
(免责声明:我没有对此进行测试。)