简体   繁体   中英

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

以上查询结果

The output is according to the expected result but with an additional column of sum(invoicetotal). I cannot figure a way to order the countries according to the sum of their imvoicetotal without using sum() function in select.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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