簡體   English   中英

找到最高的金額,盡管有不止一個達到相同的金額

[英]Find the highest amount although there are more than one achieving the same amount

我想為每個國家找到最好的客戶,盡管一個國家有兩個客戶數量相同,我希望他們都出現。

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;

使用 window 函數:

select c.*
from (select c.country, c.customerid, c.firstname c.lastname, sum(i.total) as total,
             dense_rank() over (partition by c.country order by sum(i.total) desc) as seqnum
      from customer c join
           invoice i
           on c.customerid = i.customerid
     ) c
where seqnum = 1;

請注意,我還介紹了 window 函數,因此查詢更易於編寫和閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM