繁体   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