簡體   English   中英

如何 select 行號為 SQL 的每個 user_id 的最大收入?

[英]How to select the max of revenue for each user_id with row number in SQL?

在我的數據集中有一些 user_id,它們每個都有幾個行號(從 1 到 n),每行都有特定的收入。 我想 select 行號的每個 user_id 的最大收入屬於此收入。 我想查詢突出顯示的行的結果。

在此處輸入圖像描述

一種方法是相關子查詢:

select t.*
from t
where t.revenue = (select max(t2.revenue) from t t2 where t2.user_id = t.user_id);

如果最大值存在並列,則返回所有最高值行。

select *,
    case when revenue = max(revenue) over (partition by user_id) then 1 else 0 end as highlight
from T
select tt.*
from #tbl tt
  join (select user_Id, max(revenue) as revenue from #tbl group by user_Id) tm on tt.user_Id = tm.user_Id and tt.revenue = tm.revenue

暫無
暫無

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

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