简体   繁体   English

通过别名列访问SQL查询ORDER

[英]Access SQL-Query ORDER BY Alias-Column

I have a question about a simple sql-query. 我对一个简单的sql查询有疑问。

This is my query in Access 2013: 这是我在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;

... and it work perfect. ...而且效果很好。 But how can I put the alias-column (TotPr) in ORDER BY? 但是,如何将别名列(TotPr)放在ORDER BY中呢?

Thank you for help :) 谢谢你的帮助 :)

You can use the formula: 您可以使用以下公式:

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);

Use SUM(Orders.TotalPrice) instead of the aliased name, you want the aggregation 使用SUM(Orders.TotalPrice)而不是别名,您需要聚合

so your query would be: 因此您的查询将是:

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);

Order By generally doesn't like calculated columns, due to the way SQL is figured internally by most engines. 由于大多数引擎在内部计算SQL的方式,Order By通常不喜欢计算列。 Try this: 尝试这个:

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;

(Disclaimer: I didn't test this.) (免责声明:我没有对此进行测试。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM