Update across schemas in Oracle - sql

I want to update a table with data from another schema. Why does this not work?
UPDATE table a
SET a.value = b.value
FROM other_schema.table b
WHERE a.id = b.id AND b.value IS NOT NULL;
I've tried this with a join on id as well, but with the same syntax error.

Oracle doesn't support a FROM clause in an UPDATE statement. You'd want something like
UPDATE table a
SET a.value = (SELECT b.value
FROM other_schema.table b
WHERE a.id = b.id
AND b.value IS NOT NULL)
WHERE EXISTS(SELECT b.value
FROM other_schema.table b
WHERE a.id = b.id
AND b.value IS NOT NULL)
You could omit the EXISTS if you want to update every row in a whether or not there is a match in b but I assume that is not your goal.
If your join produces a key-preserved result, you could also
UPDATE( SELECT a.id, a.value a_value, b.value b_value
FROM table a
JOIN other_schema.table b
ON a.id = b.id AND
b.value IS NOT NULL )
SET a_value = b_value;

Related

Postgres how do I update all rows in a table with left join and case?

Assume that I have table A, and table B. Table B contains some rows from table A
How do I run an update like the following?
update table A
set A.column = case when B.id is present then B.column else null
from B where B.id = A.id OR B is null
EDIT
The original query is incomplete, this is the updated one i need help with
update table A
set A.column1 = case when B.id is present then B.column else null end,
set A.column2 = case when B.id is present then null else A.id end
from B
where B.id = A.id OR B is null
You can simply use a subquery:
UPDATE A SET A.column = (
SELECT B.column FROM B WHERE B.id = A.id
)
You can use a left join in the FROM clause as well to update multiple columns:
UPDATE A SET column1 = B.column1, column2 = B.column2
FROM A x LEFT JOIN B ON x.id = B.id
WHERE A.id = x.id
The table A in the FROM clause is only to allow a left join with B.

Unable to apply case statement in join condition

I have two tables TableA and TableB. For one of the record in TableB(id =1), I want to perform join on condition1(a.value = b.value) and for other records I want to join on condition2((a.value - b.value)/ a.val < 1).
But, I am getting syntax error at end. How to apply case condition in this scenario?
Select * from TableA a
LEFT JOIN TableB b
on a.id = b.id
and (
case when b.id = 1 then a.value = b.value
else (a.value - b.value)/ a.val < 1 end
)
Just use boolean logic:
select *
from TableA a left join
TableB b
on a.id = b.id and
(b.id = 1 and a.value = b.value or
b.id <> 1 and (a.value - b.value)/ a.val < 1
)

SQL query to select from one table based on a column value in other table

I have two tables and I want to select all values from "TABLE A" that have a different value in a column from "TABLE B".
I tried this
SELECT A.* FROM tableA A
left join tableB B ON A.id = B.id WHERE B.column <> 1;
But this just return the value that I want to ignore.
SELECT A.*
FROM tableA A
INNER JOIN tableB B
ON A.id = B.id
WHERE B.column != 1;
or
SELECT A.* FROM tableA A WHERE A.Id NOT IN (SELECT B.Id FROM tableB B WHERE B.column != 1)
Depends on your SQL you can use <> or !=
I would suggest not exists:
SELECT A.*
FROM tableA A
WHERE NOT EXISTS (SELECT 1 FROM tableB B WHERE A.id = B.id AND B.column = 1);
Using a JOIN can result in duplicated rows, if more than one row in B matches the JOIN condition.

Updating table with joining to a second table

I'm trying to update the value in column 'ID' from table 1 with the value in column 'ID' from table 2 - Only if they do not match. I think I have everything except the set statement down.
I'm wondering if this is the best way to go about it and how to format the sub-query for this type of problem
update table1 B
set B.id = (select A.id
from table2 A
where B.num = A.num
and B.name = A.name)
where B.num = A.num
and B.name = A.name
and B.id <> A.id
;
Maybe something like this?
update B
set B.id=A.Id
from table1 B
join table2 A
on B.num=A.num
and B.name=A.name
and B.id<>A.id
Oracle does not support join in an update. But you can use two subqueries:
update table1 B
set id = (select A.id
from table2 A
where B.num = A.num and
B.name = A.name
)
where exists (select A.id
from table2 A
where B.num = A.num and
B.name = A.name and
B.id <> A.id
);
Use the MERGE command.
Here is a basic example:
MERGE INTO table1 a
USING (SELECT bb.asdf,cc.zxcv
from table2 bb,table3 cc
where bb.field1=cc.field1) b
ON (a.asdf=b.asdf)
WHEN MATCHED THEN
UPDATE SET a.zxcv=b.zxcv;

How to use case when in sql query

query="SELECT a.id, b.id
FROM tab_a a, tab_b b
WHERE a.ref = b.ref
AND a.amount = -b.amount
AND NOT a.tot AND NOT b.tot
AND a.a_id = %(a_id)s AND b.a_id = %(a_id)s
{p_id_condition}
ORDER BY a.date desc"
i am trying
first try to match the ref, but if no pair found, try to match the amount
Would something like this work:
SELECT
a.id,
b.id
FROM tab_a a
INNER JOIN tab_b b
ON a.ref = b.ref OR
a.amount = -b.amount
WHERE
NOT a.tot AND
NOT b.tot AND
a.a_id = %(a_id)s AND
b.a_id = %(a_id)s
{p_id_condition}
ORDER BY a.date DESC
I rewrote your query using explicit join syntax, which isolated and revealed the two join conditions you mentioned in your question. Then I changed the and to an or, which would seem to be what you want.