【问题标题】:Update a column with the value from previous row based on Previous rows使用前一行的值更新一列
【发布时间】:2018-09-11 08:47:43
【问题描述】:

我有一个包含 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

【问题讨论】:

  • 我不明白你的情况If any ID has latest date record as Inside_Churn =1。您为 302341-210 更改的行不是此键的最新行。你为什么选择那个而不是最后一行作为钥匙?
  • 抱歉错误的描述它不是最新的(试图说记录在前)我们需要考虑最旧的日期。

标签: sql-server tsql


【解决方案1】:

以下脚本将显示您所追求的结果。仍然不相信我已经理解了这个问题,但它会将你之前的数据变成你需要的数据,所以希望能涵盖它。

第一部分是创建测试表:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多