首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子查询返回1个以上的值-使用特定日期的值更新日期范围内的记录

子查询返回1个以上的值-使用特定日期的值更新日期范围内的记录
EN

Stack Overflow用户
提问于 2021-01-23 06:56:09
回答 1查看 31关注 0票数 1

我正在尝试更新'2020-10-12‘之前的记录的LocationNumber值。更新的记录应该是那些LocationNumber不等于'2020-10-10‘的记录。在12号之后,某些记录的LocationNumber值变得不正确,所以我正在尝试修复它们。

编辑:记录必须存在于'2020-10-10',我不想更新在那个日期系统中没有的新记录。

以下是我尝试过的选项:

代码语言:javascript
复制
update ReadHeader as z0
set locationnumber = z1.locationnumber
from ReadHeader as z1
where z1.identifier = z0.identifier
and z1.LocationNumber in(select LocationNumber from ReadHeader where ReadLogDate = '2020-10-10')
and z0.LocationNumber in(select LocationNumber from ReadHeader where ReadLogDate > '2020-10-12')
and z0.LocationNumber not in(select LocationNumber from ReadHeader where ReadLogDate = '2020-10-10')




update ReadHeader SET LocationNumber = (SELECT LocationNumber from ReadHeader where ReadLogDate = '2020-10-10')
where ReadLogDate > '2020-10-12' 
and LocationNumber not in(select LocationNumber from ReadHeader where ReadLogDate = '2020-10-10')

样本数据,有成千上万的唯一“标识符”,每天都有一个新的读取,我只想修复那些在'2020-10-12‘之后LocationNumbers不正确的。

代码语言:javascript
复制
Identifier       LocationNumber       AcctNum       ReadLogDate    Read Value

T108468264          336086           94103494      '2020-10-10'        5
T108468264          336086           94103494      '2020-10-11'        8
T108468264          888888           94103494      '2020-10-12'        19
T108468264          888888           94103494      '2020-10-13'        25
T108468264          888888           94103494      '2020-10-14'        30

所需结果:

代码语言:javascript
复制
Identifier       LocationNumber       AcctNum       ReadLogDate    Read Value

T108468264          336086           94103494      '2020-10-10'        5
T108468264          336086           94103494      '2020-10-11'        8
T108468264          336086           94103494      '2020-10-12'        19
T108468264          336086           94103494      '2020-10-13'        25
T108468264          336086           94103494      '2020-10-14'        30

如有任何帮助,我们不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-23 07:54:06

嗯。。。使用可更新的CTE:

代码语言:javascript
复制
with toupdate as (
      select rh.*,
             max(case when ReadLogDate = '2020-10-12' then locationnumber end) over (partition by identifier) as locationnumber_20201012
      from ReadHeader rh
     )
update toupdate
    set locationnumber = locationnumber_20201012
    where readlogdate > '2020-10-12';
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65853863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档