如何比较前几行的日期并更新 SQL 服务器中的条目
How to compare dates from previous rows and update entries in SQL Server
我一直在努力尝试通过比较前几行的日期来找到更新某些条目的方法。
这是 table 中存储的日期示例 我愿意更新:
ContractId PartnerId DocumentState DealsDate ActualCloseDate
-------------------------------------------------------------------
119577922 1450216 38 2016-04-21 2017-08-01
222138372 1450216 38 2017-11-22 2019-04-01
223328932 1450216 38 2018-07-30 2018-11-19
224263667 1450216 38 2019-01-15 2019-04-19
225286013 1450216 38 2019-06-21 2019-07-19
225704493 1450216 38 2019-08-30 2019-12-11
目标是将所有 ContractId
的 DocumentState
更改为 36,因为它之前的任何条目的 ActualCloseDate
大于其 DealsDate
.
输出应如下所示:
ContractId PartnerId DocumentState DealsDate ActualCloseDate
-------------------------------------------------------------------
119577922 1450216 38 2016-04-21 2017-08-01
222138372 1450216 38 2017-11-22 2019-04-01
223328932 1450216 36 2018-07-30 2018-11-19
224263667 1450216 36 2019-01-15 2019-04-19
225286013 1450216 38 2019-06-21 2019-07-19
225704493 1450216 38 2019-08-30 2019-12-11
下面是将数据插入临时文件的代码 table。
create table #Test
(
ContractId int,
PartnerId int,
DocumentState int,
DeasDate datetime,
ActualCloseDate datetime
)
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (119577922, 1450216, 38, '2016-04-21', 2017-08-01')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (222138372, 1450216, 38, '2017-11-22', 2019-04-01')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (223328932, 1450216, 38, '2018-07-30', 2018-11-19')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (224263667, 1450216, 38, '2019-01-15', 2019-04-19')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (225286013, 1450216, 38, '2019-06-21', 2019-07-19')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (225704493, 1450216, 38, '2019-08-30', 2019-12-11')
提前致谢!
干杯,
我认为可更新的 CTE 可以满足您的需求:
with toupdate as (
select t.*,
max(ActualCloseDate) over (partition by PartnerId
order by dealsDate
rows between unbounded preceding and 1 preceding
) as prev_max_acd
from #test t
)
update toupdate
set documentstate = 36
where prev_max_acd > dealsdate;
Here 是一个 db<>fiddle
你可以试试这个
update Test set DocumentState = 36
where exists (
select * from Test inrTest
where inrTest.ContractId < Test.ContractId
and inrTest.ActualCloseDate > Test.DealsDate
)
如果订单不在"ContractId",那么只需将合约id条件更改为您想要订购的列
我一直在努力尝试通过比较前几行的日期来找到更新某些条目的方法。
这是 table 中存储的日期示例 我愿意更新:
ContractId PartnerId DocumentState DealsDate ActualCloseDate
-------------------------------------------------------------------
119577922 1450216 38 2016-04-21 2017-08-01
222138372 1450216 38 2017-11-22 2019-04-01
223328932 1450216 38 2018-07-30 2018-11-19
224263667 1450216 38 2019-01-15 2019-04-19
225286013 1450216 38 2019-06-21 2019-07-19
225704493 1450216 38 2019-08-30 2019-12-11
目标是将所有 ContractId
的 DocumentState
更改为 36,因为它之前的任何条目的 ActualCloseDate
大于其 DealsDate
.
输出应如下所示:
ContractId PartnerId DocumentState DealsDate ActualCloseDate
-------------------------------------------------------------------
119577922 1450216 38 2016-04-21 2017-08-01
222138372 1450216 38 2017-11-22 2019-04-01
223328932 1450216 36 2018-07-30 2018-11-19
224263667 1450216 36 2019-01-15 2019-04-19
225286013 1450216 38 2019-06-21 2019-07-19
225704493 1450216 38 2019-08-30 2019-12-11
下面是将数据插入临时文件的代码 table。
create table #Test
(
ContractId int,
PartnerId int,
DocumentState int,
DeasDate datetime,
ActualCloseDate datetime
)
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (119577922, 1450216, 38, '2016-04-21', 2017-08-01')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (222138372, 1450216, 38, '2017-11-22', 2019-04-01')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (223328932, 1450216, 38, '2018-07-30', 2018-11-19')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (224263667, 1450216, 38, '2019-01-15', 2019-04-19')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (225286013, 1450216, 38, '2019-06-21', 2019-07-19')
insert into #Test (ContractId, PartnerId, DocumentState, DealsDate, ActualCloseDate) values (225704493, 1450216, 38, '2019-08-30', 2019-12-11')
提前致谢!
干杯,
我认为可更新的 CTE 可以满足您的需求:
with toupdate as (
select t.*,
max(ActualCloseDate) over (partition by PartnerId
order by dealsDate
rows between unbounded preceding and 1 preceding
) as prev_max_acd
from #test t
)
update toupdate
set documentstate = 36
where prev_max_acd > dealsdate;
Here 是一个 db<>fiddle
你可以试试这个
update Test set DocumentState = 36
where exists (
select * from Test inrTest
where inrTest.ContractId < Test.ContractId
and inrTest.ActualCloseDate > Test.DealsDate
)
如果订单不在"ContractId",那么只需将合约id条件更改为您想要订购的列