I’ve looked all over, but can’t figure out how to update a column in a table based on subquery data when matching on a customer ID. Here’s some syntax to give an idea of what I’m trying to do:
UPDATE TableName
SET TableName.Revenue = Z.Revenue
FROM
(
SELECT
CustomerID,
sum(Revenue) as Revenue
FROM
(
SELECT
CustomerID,
Revenue
FROM
TableA
WHERE
CustomerID in TableF
UNION ALL
SELECT
CustomerID,
Revenue
FROM
TableB
WHERE
CustomerID in TableF
)
GROUP BY
CustomerID
) Z
WHERE
TableName.CustomerID = Z.CustomerID
In essence, I’m looking to update a table column if another ID column under the same table matches an ID from a subquery. My goal is to avoid creating a whole new table from the subquery. Any help would be appreciated. Thanks.
Oracle does not support a FROM clause for an UPDATE. This will work:
Another option is to use the dml_expression_table syntax, which basically looks something like
update (select a.x, b.y from a join b on a.a = b.b) set x = y. But that’s kinda weird and requires unique constraints to work.Or you could use MERGE with only an UPDATE section. It’s unusual to use a MERGE for only an UPDATE, but since Oracle supports the ANSI standard it may help you use familiar syntax.