根据要在此之后更新行的结果,我有一个包含Inside_churn的表。 源表如下所示:
soli snapshotdate Madchangeflag Within15days crdpushpullflag Inside_Churn
302341-210 2018-08-13 00:00:00.000 Y Y PO 0
302341-210 2018-08-14 00:00:00.000 Y N PI 0
302341-210 2018-08-15 00:00:00.000 Y Y PI 1
302341-210 2018-08-16 00:00:00.000 Y Y PO 0
302341-210 2018-08-17 00:00:00.000 Y N PI 0
302341-210 2018-08-18 00:00:00.000 N N PI 0
302341-210 2018-08-19 00:00:00.000 Y Y PI 1
302341-210 2018-08-20 00:00:00.000 Y Y PO 0
602341-110 2018-08-13 00:00:00.000 Y Y PO 0
602341-110 2018-08-14 00:00:00.000 Y N PI 0
602341-110 2018-08-15 00:00:00.000 Y Y PI 1
602341-110 2018-08-16 00:00:00.000 Y Y PO 0
602341-110 2018-08-17 00:00:00.000 Y N PI 0
602341-110 2018-08-18 00:00:00.000 N N PI 0
602341-110 2018-08-19 00:00:00.000 Y Y PI 1
602341-110 2018-08-20 00:00:00.000 Y Y PO 0
如果任何ID的最新日期记录为Inside_Churn = 1,则随后具有Madchangeflag ='Y'和crdpushpullflag ='PI'的记录应更新为Inside_Churn = 1
预计产量:
soli snapshotdate Madchangeflag Within15days crdpushpullflag Inside_Churn
302341-210 2018-08-13 00:00:00.000 Y Y PO 0
302341-210 2018-08-14 00:00:00.000 Y N PI 0
302341-210 2018-08-15 00:00:00.000 Y Y PI 1
302341-210 2018-08-16 00:00:00.000 Y Y PO 0
302341-210 2018-08-17 00:00:00.000 Y N PI 1
302341-210 2018-08-18 00:00:00.000 N N PI 0
302341-210 2018-08-19 00:00:00.000 Y Y PI 1
302341-210 2018-08-20 00:00:00.000 Y Y PO 0
602341-110 2018-08-13 00:00:00.000 Y Y PO 0
602341-110 2018-08-14 00:00:00.000 Y N PI 0
602341-110 2018-08-15 00:00:00.000 Y Y PI 1
602341-110 2018-08-16 00:00:00.000 Y Y PO 0
602341-110 2018-08-17 00:00:00.000 Y N PI 1
602341-110 2018-08-18 00:00:00.000 N N PI 0
602341-110 2018-08-19 00:00:00.000 Y Y PI 1
602341-110 2018-08-20 00:00:00.000 Y Y PO 0
答案 0 :(得分:0)
以下脚本将显示您想要的结果。仍然不相信我已经理解了这个问题,但是它将把您之前的数据变成您所需的数据,因此希望可以解决它。
第一部分是创建测试表:
declare @t table(soli varchar(20), snapshotdate date, Madchangeflag char(1), Within15days char(1), crdpushpullflag char(2), Inside_Churn int)
insert @t values ('302341-210','2018-08-13','Y','Y','PO',0)
, ('302341-210','2018-08-14','Y','N','PI',0)
, ('302341-210','2018-08-15','Y','Y','PI',1)
, ('302341-210','2018-08-16','Y','Y','PO',0)
, ('302341-210','2018-08-17','Y','N','PI',0)
, ('302341-210','2018-08-18','N','N','PI',0)
, ('302341-210','2018-08-19','Y','Y','PI',1)
, ('302341-210','2018-08-20','Y','Y','PO',0)
, ('602341-110','2018-08-13','Y','Y','PO',0)
, ('602341-110','2018-08-14','Y','N','PI',0)
, ('602341-110','2018-08-15','Y','Y','PI',1)
, ('602341-110','2018-08-16','Y','Y','PO',0)
, ('602341-110','2018-08-17','Y','N','PI',0)
, ('602341-110','2018-08-18','N','N','PI',0)
, ('602341-110','2018-08-19','Y','Y','PI',1)
, ('602341-110','2018-08-20','Y','Y','PO',0)
然后执行更新并显示结果:
update t set inside_churn=1
from @t t
where t.Madchangeflag='y' and t.crdpushpullflag='PI' and t.Inside_Churn=0
-- Make sure this is the last target row
and not exists(
select * from @t t2
where t2.soli=t.soli
and t2.Madchangeflag='y' and t2.crdpushpullflag='PI' and t2.Inside_Churn=0
and t2.snapshotdate>t.snapshotdate
)
-- Make sure there is a later row with inside_churn=1
and exists(
select * from @t t3
where t3.soli=t.soli
and t3.Madchangeflag='y' and t3.crdpushpullflag='PI' and t3.Inside_Churn=1
and t3.snapshotdate>t.snapshotdate
)
select * from @t