[英]Update Oracle SQL - table with values from duplicates
i hope that somebody could help. 我希望有人可以帮忙。 I need to update a table from a select with duplicates. 我需要从具有重复项的选择中更新表。
ID;CLASS;VALUE;NEW
1;a;a3;
1;b;s6;
1;c;b99;
2;a;s3;
2;b;r6;
2;c;b99;
3;a;s5;
4;a;r6;
4;b;a3;
Look at my example table, there is a colum NEW which i have to update. 看看我的例子表中,有一个新的科拉姆,我必须更新。 In the example the column NEW was filled manually. 在示例中,“ 新 ”列是手动填充的。
Here is the goal (as shown in table col NEW): 这是目标(如表col NEW所示):
1.find duplicates via ID (HAVING COUNT(*) >1 or something like that) 1.通过ID(HAVING COUNT(*)> 1或类似的东西)查找重复项
Easy for you? 容易吗?
Thx in advance 提前Thx
The logic behind is not completely clear; 背后的逻辑尚不完全清楚。 this could be a way. 这可能是一种方法。
setup: 设定:
create table yourTable(id, class, value, new) as
(
select 1, 'a', 'a3', cast (null as varchar2(10)) from dual union all
select 1, 'b', 's6', null from dual union all
select 1, 'c', 'b99', null from dual union all
select 2, 'a', 's3', null from dual union all
select 2, 'b', 'r6', null from dual union all
select 2, 'c', 'b99', null from dual union all
select 3, 'a', 's5', null from dual union all
select 4, 'a', 'r6', null from dual union all
select 4, 'b', 'a3', null from dual
)
query: 查询:
merge into yourTable t1
using (
select listagg(value, '_') within group (order by class) as new,
id
from yourTable
where class in ('a', 'b')
group by id
having count(distinct class) = 2
) t2
on ( t1.id = t2.id
and t1.class in ('a', 'b')
)
when matched then
update set t1.new = t2.new
result: 结果:
SQL> select *
2 from yourTable;
ID C VAL NEW
---------- - --- ----------
1 a a3 a3_s6
1 b s6 a3_s6
1 c b99
2 a s3 s3_r6
2 b r6 s3_r6
2 c b99
3 a s5
4 a r6 r6_a3
4 b a3 r6_a3
9 rows selected.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.