1

I was wondering how can I change my query to JOIN one with the same result:

update CDR 
set CDR_TYPE = 'ON_NET' 
where anum in (select subscriber 
               from DEGREE 
               where sub_type = 'MTN')
  and bnum in (select subscriber 
               from DEGREE 
               where sub_type = 'MTN')

FYI: I am using ORACLE database.

Best Regards.

Ashish Gaur
  • 2,000
  • 2
  • 16
  • 32
Ali
  • 1,609
  • 1
  • 23
  • 57

2 Answers2

1

You could use a WITH clause to get rid of the repeated subquery.

WITH subquery AS (select subscriber 
               from DEGREE 
               where sub_type = 'MTN')
UPDATE cdr
SET cdr_type = 'ON_NET' 
WHERE anum IN (subquery)
  AND bnum IN (subquery);
Devon
  • 32,773
  • 9
  • 61
  • 91
0
UPDATE 
(SELECT cdr_type 
 FROM cdr c
 INNER JOIN degree d
 ON c.anum = d.subscriber 
    AND c.bnum = d.subscriber
 WHERE d.sub_type = 'MTN'
) t
SET t.cdr_type = 'ON_NET'
Mudassir Hasan
  • 26,910
  • 19
  • 95
  • 126
  • Just as an onlooker, that is a pretty interesting query. Mind explaining how the subquery works in it? – Devon Apr 19 '14 at 07:25
  • It should update more than 52m records while it just update 18k of them! – Ali Apr 19 '14 at 08:26