我使用窗口函数创建了一个名为custom_rank的列。有没有办法获得每个用户ID的特定列的最小值和最大值?我基本上试图在他们的注册期内获得给定用户的最小和最大权重。
select top 1000
c.weight, c.units,
rank() over (partition by uuid, enrolled_on order by enrolled_on desc, input_date asc) as "weight_rank"
from tableA a
join tableB b
on (b.member_no = a.member_no)
join tableC c
on (c.userId = b.uuid)
where input_date >= enrolled_on and input_date < cancel_date
答案 0 :(得分:2)
使用CTE,然后获得每个用户的最大/最小值。
with mm as
(
select top 1000
c.weight, c.units, c.userId
rank() over (partition by uuid, enrolled_on order by enrolled_on desc, input_date asc) as "weight_rank"
from tableA a
join tableB b
on (b.member_no = a.member_no)
join tableC c
on (c.userId = b.uuid)
where input_date >= enrolled_on and input_date < cancel_date
)
select userId, min(weight_rank), max(weight_rank)
from mm
group by userId;
答案 1 :(得分:0)
只需使用max()
和min()
作为窗口函数:
with cte as (
select c.weight, c.units, uuid,
rank() over (partition by uuid, enrolled_on
order by enrolled_on desc, input_date asc) as weight_rank
from tableA a join
tableB b
on b.member_no = a.member_no join
tableC c
on c.userId = b.uuid
where input_date >= enrolled_on and input_date < cancel_date
)
select top 1000 cte.*,
max(weighted_rank) over (partition by uuid) as max_weightedrank_user,
min(weighted_rank) over (partition by uuid) as min_weightedrank_user,
from cte;