【发布时间】:2019-10-28 15:51:26
【问题描述】:
我有一个音乐商店的数据库,我需要从每个国家/地区的特定客户那里提取最大购买价值。在使用MAX 函数时,我注意到我在“英国”中有两个最大值的关系。所以,我需要我的查询来返回这个国家的两个客户。
With t1 As (
Select i.CustomerId, c.FirstName, c.LastName,
i.BillingCountry Country, Sum(i.Total) Totals
From Invoice i
Join Customer c
On c.CustomerId = i.CustomerId
GROUP BY 1, 4)
Select CustomerId, FirstName, LastName, Country, Max(Totals) TotalSpent
From t1
Group By 4;
这是输出
这就是输出应该是什么
我尝试使用TOP,但显然工作区不接受此功能。所以,请提出一个不使用此功能的解决方案。
提前致谢。
【问题讨论】:
标签: sql max aggregation