尝试合并时合并条件在 oracle 中不起作用
Merge condition not working in oracle when trying to merge
我有一个 table 作为:
--------------
|id descrip |
|1 A |
|2 B |
|3 C |
|3 D |
|3 E |
|4 F |
|4 F |
我的预期输出是:
--------------
|id descrip |
|1 A |
|2 B |
|3C C |
|3D D |
|3E E |
|4 F |
|4 F |
我尝试使用第一种方法:
merge into rules_table a
using rules_table b
on (a.id = b.id and a.descrip and a.rowid > b.rowid)
when matched then
update set a.id = b.id || b.descrip
我尝试的第二种方法是:
update rules_table a
set a.id =
(select b.id || b.descrip
from rules_table b
where b.id = a.id
and b.descrip = a.descrip
and a.rowid = b.rowid)
但是,我没有得到想要的输出。
根据您的预期结果,我认为您正在寻找以下内容:
MERGE INTO rules_table a
USING (SELECT rowid, id, descrip, COUNT(DISTINCT descrip) over (PARTITION BY id) cnt
FROM rules_table) b
ON (a.rowid = b.rowid AND b.cnt > 1)
WHEN MATCHED THEN UPDATE
SET a.id=b.id||b.descrip
以下也应该有效:
UPDATE rules_table
SET id = id||descrip
WHERE id IN (SELECT id
FROM rules_table
GROUP BY id
HAVING COUNT(DISTINCT descrip) > 1)
我有一个 table 作为:
--------------
|id descrip |
|1 A |
|2 B |
|3 C |
|3 D |
|3 E |
|4 F |
|4 F |
我的预期输出是:
--------------
|id descrip |
|1 A |
|2 B |
|3C C |
|3D D |
|3E E |
|4 F |
|4 F |
我尝试使用第一种方法:
merge into rules_table a
using rules_table b
on (a.id = b.id and a.descrip and a.rowid > b.rowid)
when matched then
update set a.id = b.id || b.descrip
我尝试的第二种方法是:
update rules_table a
set a.id =
(select b.id || b.descrip
from rules_table b
where b.id = a.id
and b.descrip = a.descrip
and a.rowid = b.rowid)
但是,我没有得到想要的输出。
根据您的预期结果,我认为您正在寻找以下内容:
MERGE INTO rules_table a
USING (SELECT rowid, id, descrip, COUNT(DISTINCT descrip) over (PARTITION BY id) cnt
FROM rules_table) b
ON (a.rowid = b.rowid AND b.cnt > 1)
WHEN MATCHED THEN UPDATE
SET a.id=b.id||b.descrip
以下也应该有效:
UPDATE rules_table
SET id = id||descrip
WHERE id IN (SELECT id
FROM rules_table
GROUP BY id
HAVING COUNT(DISTINCT descrip) > 1)