使用来自另一个 table 的重复值更新 Oracle table 中的列
Update column in Oracle table with value from another table with duplicates
我正在尝试将列 (REPT_IND) 从 table A 更新为 table B 中的值,其中 A.ID = B.ID 和一些table B 中的条件。
table B 中有一些重复项,但是 REPT_IND 是相同的,我仍然需要该值。
我如何在 Oracle 上执行此操作?感谢任何提示,谢谢!
以下代码有错误:
ORA-01427: single-row subquery returns more than one row
代码:
UPDATE A
SET REPT_IND= (
SELECT B.REPT_IND
FROM B
INNER JOIN A
ON B.ID = A.ID
where A.ID = B.ID
and B.job_type = 'P'
and B.FT_PT is not null
);
只需使用相关子查询。 . .并且不要在子查询中重复 table 引用:
UPDATE A
SET REPT_IND = (SELECT B.REPT_IND
FROM B
WHERE B.ID = A.ID AND
B.job_type = 'P' AND
B.FT_PT is not null AND
rownum = 1
);
考虑:
update a
set rept_ind= (
select max(b.rept_ind)
from b
where
a.id = b.id
and b.job_type = 'p'
and b.ft_pt is not null
);
不需要在子查询中再次join
table a
- 关联子句就足够了。您可以通过打开聚合来解决可能的重复问题,这保证只会返回一行。
您也可以在子查询中使用 select distinct
而不是 select max(...)
。这在某种程度上更准确,因为它确实确保多行具有相同的 rept_ind
(如果它们不相同,那么您仍然会得到 ORA-01427
错误)。
您也可以尝试 merge
语句:
merge into a
using (
select a.id,max(b.rept_ind) rept_ind
from a left join b on a.id=b.id
where b.job_type = 'p'
and b.ft_pt is not null
) b
on (a.id=b.id)
when matched then update
set a.rept_ind=b.rept_ind;
或者如果您不想将 a.rept_ind 设置为 null,如果 b 中没有相关行:
merge into a
using (
select b.id, max(b.rept_ind) rept_ind
from b
where
b.job_type = 'p'
and b.ft_pt is not null
group by b.id
) b
on (a.id=b.id)
when matched then update
set a.rept_ind=b.rept_ind;
我正在尝试将列 (REPT_IND) 从 table A 更新为 table B 中的值,其中 A.ID = B.ID 和一些table B 中的条件。 table B 中有一些重复项,但是 REPT_IND 是相同的,我仍然需要该值。 我如何在 Oracle 上执行此操作?感谢任何提示,谢谢!
以下代码有错误:
ORA-01427: single-row subquery returns more than one row
代码:
UPDATE A
SET REPT_IND= (
SELECT B.REPT_IND
FROM B
INNER JOIN A
ON B.ID = A.ID
where A.ID = B.ID
and B.job_type = 'P'
and B.FT_PT is not null
);
只需使用相关子查询。 . .并且不要在子查询中重复 table 引用:
UPDATE A
SET REPT_IND = (SELECT B.REPT_IND
FROM B
WHERE B.ID = A.ID AND
B.job_type = 'P' AND
B.FT_PT is not null AND
rownum = 1
);
考虑:
update a
set rept_ind= (
select max(b.rept_ind)
from b
where
a.id = b.id
and b.job_type = 'p'
and b.ft_pt is not null
);
不需要在子查询中再次join
table a
- 关联子句就足够了。您可以通过打开聚合来解决可能的重复问题,这保证只会返回一行。
您也可以在子查询中使用 select distinct
而不是 select max(...)
。这在某种程度上更准确,因为它确实确保多行具有相同的 rept_ind
(如果它们不相同,那么您仍然会得到 ORA-01427
错误)。
您也可以尝试 merge
语句:
merge into a
using (
select a.id,max(b.rept_ind) rept_ind
from a left join b on a.id=b.id
where b.job_type = 'p'
and b.ft_pt is not null
) b
on (a.id=b.id)
when matched then update
set a.rept_ind=b.rept_ind;
或者如果您不想将 a.rept_ind 设置为 null,如果 b 中没有相关行:
merge into a
using (
select b.id, max(b.rept_ind) rept_ind
from b
where
b.job_type = 'p'
and b.ft_pt is not null
group by b.id
) b
on (a.id=b.id)
when matched then update
set a.rept_ind=b.rept_ind;