简体   繁体   English

如何在不使用sum函数的情况下对sum进行分组?

[英]How to group by sum without using sum function in select?

There's a customer table having id, firstname, country and invoice total( for each customer). 有一个客户表,其中包含ID,名字,国家和发票总数(每个客户)。 I need to write a query displaying list of countries (in descending order) according to revenue generated by each country. 我需要编写一个查询,以根据每个国家/地区产生的收入显示国家/地区列表(降序排列)。 预期结果 I'm trying the following the following query: 我正在尝试以下查询:

Select country, sum(invoicetotal) from customer group by country order by 2 desc 选择国家,按国家/地区顺序从客户组中选择总和(发票总额)2 desc

以上查询结果

The output is according to the expected result but with an additional column of sum(invoicetotal). 输出是根据预期的结果,但还有一列sum(发票总额)。 I cannot figure a way to order the countries according to the sum of their imvoicetotal without using sum() function in select. 我无法找到一种方法,无需在select中使用sum()函数,即可根据国家的不记账总额进行排序。

You should be able to do: 您应该能够:

select country
from t
group by country
order by sum(revenue) desc;

I think all databases support the use of aggregation functions in the order by even if the value is not in the select . 我认为order by即使值不在select所有数据库也支持按order by使用聚合函数。

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

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