Based on another answer to a correlated update of a stored key, here is another possible opportunity to use CTE with an update in Oracle SQL, avoiding duplication of the where clause:
update (
with cte as (select avg(salary) average_salary from instructor)
select id, salary, cte.average_salary from instructor cross join cte
where <some_condition>
)
set salary = case
when salary <= average_salary/2 then salary * 1.1
when salary <= average_salary then salary * 1.05
else salary * 1.03
end
In the case of self-connecting, this can be simplified to the CTE-less version:
update (
select id, salary, (select avg(salary) from instructor) average_salary
from instructor
where <some_condition>
)
set salary = case
when salary <= average_salary/2 then salary * 1.1
when salary <= average_salary then salary * 1.05
else salary * 1.03
end
source
share