简体   繁体   English

DB2 LUW MERGE 使用同一张表更新不同的行

[英]DB2 LUW MERGE using same table to update a different row

My table data looks like this screen shot我的表格数据看起来像这个屏幕截图

My poorly attempted SQL is this...我尝试不佳的 SQL 是这个...
MERGE INTO PRINT target MERGE INTO PRINT 目标
USING使用
( (
select ID,PDF_FILE select ID,PDF_FILE
from PRINT从打印
where date(PRINTED) = '2022-01-06'其中日期(打印)='2022-01-06'
and PDF_FILE is not null并且 PDF_FILE 不是 null
) sause ) 香肠
ON (target.ID = sause.ID)开(目标.ID = sause.ID)
WHEN MATCHED THEN当匹配然后
UPDATE SET target.PDF_FILE = sause.PDF_FILE UPDATE SET target.PDF_FILE = sause.PDF_FILE

It is updating all rows in the table.它正在更新表中的所有行。 I do not want this.我不想要这个。

How can I make it ONLY update the 1 latest PRINTED row which has an empty PDF_FILE?我怎样才能让它只更新具有空 PDF_FILE 的 1 个最新的 PRINTED 行?

The idea is to enumerate target rows and update only the 1-st one.这个想法是枚举目标行并仅更新第一行。

MERGE INTO 
(
SELECT ID, PDF_FILE, ROW_NUMBER () OVER (PARTITION BY ID ORDER BY PRINTED DESC) AS RN_ 
FROM PRINT
WHERE
-- Below is an appropriate condition for
-- the target rows to distinguish them from the source row

-- PDF_FILE IS NULL
date (PRINTED) <> '2022-01-06'
) target
USING
(
select ID,PDF_FILE
from PRINT
where date (PRINTED) = '2022-01-06'
and PDF_FILE is not null
) sause
ON target.ID = sause.ID AND target.RN_ = 1 
WHEN MATCHED THEN
UPDATE SET target.PDF_FILE = sause.PDF_FILE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM