DB2 LUW MERGE 使用相同的 table 来更新不同的行
DB2 LUW MERGE using same table to update a different row
我的 table 数据是这样的
我的拙劣尝试SQL是这个...
MERGE INTO PRINT 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)
WHEN MATCHED THEN
UPDATE SET target.PDF_FILE = sause.PDF_FILE
它正在更新 table 中的所有行。我不要这个。
我怎样才能让它只更新 1 个空 PDF_FILE 的最新 PRINTED 行?
想法是枚举目标行并仅更新第一行。
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
我的 table 数据是这样的
我的拙劣尝试SQL是这个...
MERGE INTO PRINT 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)
WHEN MATCHED THEN
UPDATE SET target.PDF_FILE = sause.PDF_FILE
它正在更新 table 中的所有行。我不要这个。
我怎样才能让它只更新 1 个空 PDF_FILE 的最新 PRINTED 行?
想法是枚举目标行并仅更新第一行。
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