|
Hi Experts,
I have two tables - Master & Temp
Master:
|
A
|
B
|
C
|
D
|
|
111111
|
123451
|
xyz
|
1111
|
|
111111
|
123452
|
|
2222
|
|
111111
|
123453
|
xyz
|
1111
|
|
222222
|
123454
|
abc
|
3333
|
|
222222
|
123455
|
def
|
1111
|
|
222222
|
123456
|
|
4444
|
|
222222
|
123457
|
|
4444
|
|
222222
|
123458
|
def
|
1111
|
Temp:
|
A
|
B
|
C
|
D
|
|
111111
|
123452
|
xyz
|
2222
|
|
222222
|
123454
|
abc22
|
3333
|
|
222222
|
123456
|
bcd
|
4444
|
|
222222
|
123457
|
bcd
|
4444
|
B is the primary constraint In both master and temp table. I have to write an update query such that:
1) When master.A = Temp.A and master.B = Temp.B and nvl(master.C,’0’) <> Temp.C, Update master.C = Temp.C
2) For master.A = Temp.A and master.B not present in Temp.B and master.C is not null, update master.C to null.
Master table is having around 5000 distinct value for A and around millions of records in total. Temp table will have around 500000 records at a time. Can we have a single Update query for this. I have worked upon a query, but that is taking too long to run:
update Master m
set m.C=
(select distinct Temp.C from Temp
where Temp.B=m.B)
where not exists (
select C from Temp
where (m.B=Temp.B and
m.C=Temp.C)
or (m.A<>Temp.A)
)
Please assist with your valuable inputs and suggestion.
Thanks,
Kunal
|