SQL帮助-根据其他记录按顺序删除记录
背景/设置: 我正在使用SQLServer2008R2;目前,在我的存储过程中,我正在使用一个临时表。我的最终目标是从字段更改列表创建覆盖率地图 下面的示例表包含在最坏情况下可能发生的覆盖率更改的所有不同场景。我正在使用的表可以这样模拟:SQL帮助-根据其他记录按顺序删除记录,sql,sql-server,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008 R2,背景/设置: 我正在使用SQLServer2008R2;目前,在我的存储过程中,我正在使用一个临时表。我的最终目标是从字段更改列表创建覆盖率地图 下面的示例表包含在最坏情况下可能发生的覆盖率更改的所有不同场景。我正在使用的表可以这样模拟: declare @tmp table ([File_Name] nvarchar(3), File_Key int, Plan_Num nvarchar(4), Eff_Date datetime, Term_Date datetime) insert in
declare @tmp table ([File_Name] nvarchar(3), File_Key int, Plan_Num nvarchar(4), Eff_Date datetime, Term_Date datetime)
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('IND', 1, 'A', '1/1/2014') -- Always delete this row, CLM is not yet A
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('CLM', 1, 'A', '2/1/2014')
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('IND', 1, 'D', '3/1/2014')
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('IND', 1, 'A', '4/1/2014')
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('CLM', 1, 'D', '5/1/2014')
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('IND', 1, 'D', '6/1/2014') -- Always delete this row, CLM is D
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('IND', 1, 'A', '7/1/2014') -- Always delete this row, CLM is D
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('CLM', 1, 'A', '8/1/2014')
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('CLM', 1, 'D', '9/1/2014') -- Key record (Remove for 2nd scenario)
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('IND', 1, 'D', '1/1/2015') -- If the key record exists delete this row, else keep
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('IND', 1, 'A', '2/1/2015') -- If the key record exists delete this row, else keep
insert into @tmp ([File_Name], File_Key, Plan_Num, Eff_Date) values ('IND', 1, 'D', '3/1/2015') -- If the key record exists delete this row, else keep
问题:
在我使用的系统中,父记录可以在不实际修改子记录的情况下激活或终止子记录的覆盖范围。因此,我必须将父记录[IND]和子记录[CLM]的字段更改结合起来,以获得完整的映射
所需帮助:
我需要的帮助是删除我不需要的[IND]记录。我只想保留上次[CLM]更改的代码值处于活动状态时发生的[IND]更改。其中一个示例记录被标记为Key Record,并且基于它的存在,覆盖范围图将发生变化。以下是所需的覆盖范围地图,包括和不包括关键记录
我需要删除:
1在CLM a和后续CLM D记录之间发生的所有IND记录,以及
2出现在CLM D记录之后且没有后续a记录的所有IND记录
删除不需要的[IND]记录后,我需要用上一个值的生效日期填充术语_Date。我有代码要做,但它需要添加一个Identity1,1列和一个自连接更新
alter table #benefits add RowID int identity(1,1)
update a set a.term_date = b.eff_date from #benefits a, #benefits b where a.file_key = b.file_key and a.RowID = b.RowID - 1
我很确定CTE也能做到这一点,但我对它们不太熟悉
多谢各位
关键记录到位后的预期结果:
没有关键记录的预期结果:
这是一个局部的解决方案,它会给你一个缺口,我想问你是否想要缺口?如果是这样,可以用另一种方法来解决 第一部分删除具有Plan_Num D的CLM的IND
delete a
from #benefits a
left join #benefits b
on a.rowid = b.rowid -1
where a.file_name = 'IND'
and b.file_name = 'CLM'
and b.Plan_num = 'D'
update a set a.term_date = b.eff_date
from #benefits a
left join #benefits b
on a.rowid = b.rowid -1
这将从@tmp中删除您不想要的记录
DELETE t FROM @tmp t
WHERE t.File_Name = 'IND'
AND ISNULL((SELECT TOP 1 Plan_Num FROM @tmp WHERE File_Name = 'CLM' AND Eff_Date < t.Eff_Date ORDER BY Eff_Date DESC),'D') = 'D'
我担心在我遇到问题的任何一种情况下,该查询都不会删除行。它实际上删除了我需要保留的一个记录。我需要删除:1在CLM a和后续CLM D记录之间出现的所有IND记录,2在CLM D记录之后出现的所有IND记录,没有子队列a记录。
delete a
from #benefits a
left join #benefits b
on a.rowid = b.rowid -1
where a.file_name = 'IND'
and b.file_name = 'CLM'
and b.Plan_num = 'D'
update a set a.term_date = b.eff_date
from #benefits a
left join #benefits b
on a.rowid = b.rowid -1
DELETE t FROM @tmp t
WHERE t.File_Name = 'IND'
AND ISNULL((SELECT TOP 1 Plan_Num FROM @tmp WHERE File_Name = 'CLM' AND Eff_Date < t.Eff_Date ORDER BY Eff_Date DESC),'D') = 'D'