I have a table1:
u_a_id d_c_s c_nm c_seq r_c_p
1 908 Test1 1 20
10 908 Test1 1 21
11 908 Test1 1 12
12759 908 Test1 1 31
12759 908 Test1 1 32
12861 878 Test2 1 43
12861 878 Test2 1 44
I have another table2:
d_c_s c_nm c_seq n_min_d_c_s n_min_c_nm
908 Test1 1 12001 Test1, First
878 Test2 1 12002 Test2, First
I need to update table1
==>
table1.c_nm
=table2.n_min_c_nm
table1.d_c_s
=table2.n_min_d_c_s
The condition to update would be:
- ONLY records where count of (u_a_id, d_c_s, c_seq) > 1 --
select u_a_id, d_c_s, c_nm, c_seq, count(*) cnt from table1 where c_nm not in ('VOID', 'WRONG') group by u_a_id, d_c_s, c_nm, c_seq having count(*) > 1;
- table1.d_c_s = table2.d_c_s
- table1.c_nm = table2.c_nm
- table1.c_seq = table2.c_seq
- Use min of r_c_p
The output would look like:
u_a_id d_c_s c_nm c_seq r_c_p
1 908 Test1 1 20
10 908 Test1 1 21
11 908 Test1 1 12
12759 12001 Test1, First 1 31
12759 908 Test1 1 32
12861 12002 Test2, First 1 43
12861 878 Test2 1 44
What would be the best way to create UPDATE/MERGE query to make this happen?
DBFIDDLE DEMO - Link