【发布时间】:2019-08-13 23:21:34
【问题描述】:
例如,我必须编写一个查询来显示在每个国家/地区花费最多的客户,但是如果一个国家/地区有两个具有相同最大值的客户,我必须在输出中显示他们两个。
我编写了返回每个国家/地区每个客户的最大值的查询,但我的示例中的最后一个国家“英国”有两个具有相同最大值的客户,我无法同时显示他们。
SELECT c1.CustomerId, c1.FirstName,c1.LastName,c1.Country,
MAX(c1.TotalSpent) as TotalSpent
FROM
(SELECT c.CustomerId,c.FirstName, c.LastName,i.BillingCountry
Country, SUM(i.Total) totalspent
FROM Customer c
JOIN Invoice i
ON c.CustomerId = i.CustomerId
GROUP BY 1
ORDER BY totalspent
) c1
GROUP BY 4
ORDER BY Country
【问题讨论】: