数据:
User ID | plan | effective date. | package
1. | P1 | 24-05-2017 | 2017
1. | P1 | 01-06-2017 | 2017
1. | P2 | 01-07 2017 | 2017
如果计划更改 - 请选择计划最近生效日期 在上面的例子中:我按日期desc
的顶级条款顺序得到它如果计划没有改变,两者都是p1行,假设只有前两行,我必须选择25-05-2017日期行。
尝试检查上一年计划与最后一行是否相同。
答案 0 :(得分:0)
听起来您想要为每个用户提供最新的软件包。如果您想为每个用户提供最新的包|计划组合,只需将plan
添加到partition by
...制作(partition by [User ID], plan order by [effective date] desc)
select
[User ID]
,[plan]
,[effective date]
,[package]
from
(select
[User ID]
,[plan]
,[effective date]
,[package]
,row_number() over (partition by [User ID] order by [effective date] dec) as rn
from YourTable) a
where rn = 1
答案 1 :(得分:0)
您可以使用以下查询:
Select top (1) with ties * from yourtable
order by row_number() over (partition by [user id] order by [effective date] desc)