22,209
社区成员
发帖
与我相关
我的任务
分享
with t1(职务代码,生效日期,描述) as
(
select '1', N'1月', 'A' union all
select '1', N'2月', 'B' union all
select '2', N'2月', 'E' union all
select '2', N'3月', 'F'
)
SELECT *,ISNULL(LEAD(生效日期)OVER(PARTITION BY 职务代码 ORDER BY 生效日期),N'无穷') AS 截止日期 FROM t1
职务代码 生效日期 描述 截止日期
1 1月 A 2月
1 2月 B 无穷
2 2月 E 3月
2 3月 F 无穷
with t1(职务代码,生效日期,描述) as
(
select '1', N'1月', 'A' union all
select '1', N'2月', 'B' union all
select '2', N'2月', 'E' union all
select '2', N'3月', 'F'
)
SELECT *,LEAD(生效日期,1,N'无穷')OVER(PARTITION BY 职务代码 ORDER BY 生效日期) AS 截止日期 FROM t1
职务代码 生效日期 描述 截止日期
1 1月 A 2月
1 2月 B 无穷
2 2月 E 3月
2 3月 F 无穷
with t1(职务代码,生效日期,描述) as
(
select '1', '1月', 'A' union all
select '1', '2月', 'B' union all
select '2', '2月', 'E' union all
select '2', '3月', 'F'
)
, t2 as
(
select 职务代码,生效日期,描述, CONVERT(int, replace(生效日期, '月', '')) 生效日期2 from t1
)
, t3 as
(
select 职务代码,生效日期,描述, 生效日期2, ROW_NUMBER() over(partition by 职务代码 order by 生效日期2) rn from t2
)
select a.职务代码, a.生效日期, a.描述
, case when b.rn is null then '无穷' else b.生效日期 end 截止日期
from t3 a left join t3 b on a.职务代码=b.职务代码 and a.rn=b.rn-1