我想为每个国家找到最好的客户尽管有一个国家有两个相同数量的客户,我希望他们都出现。
select customerid,firstname,lastname,country, max(total_amt)
from (select invoice.customerid, customer.firstname,lastname,
sum(invoice.total)total_amt,customer.country
from invoice
join customer
on customer.customerid= invoice.customerid
group by invoice.customerid,customer.country)t2
group by country;
1条答案
按热度按时间chhkpiq41#
使用窗口功能:
注意,我还引入了窗口函数,因此查询更易于编写和读取。