Netezza UPDATE from one table to another - sql

This is my query that does not work in Netezza:
UPDATE TABLE1 A
SET A.COL1= (SELECT DISTINCT B.COL1 FROM TABLE2 B WHERE B.ID= A.ID AND B.DeptID=104)
WHERE A.DeptID=3
How do I re-write this query?
Please help.

UPDATE TABLE1 A
SET A.COL1 = B.COL1
FROM TABLE2 B
WHERE
A.ID = B.ID AND
A.DeptID = 3 AND
B.DeptID = 104;

Related

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;

Oracle Query Update From [duplicate]

This question already has answers here:
Oracle SQL: Update a table with data from another table
(7 answers)
Closed 4 years ago.
I wonder if I can do this query in Oracle database?
UPDATE
Table_A SET
Table_A.col1 = Table_B.col1,
Table_A.col2 = Table_B.col2 FROM
Some_Table AS Table_A
INNER JOIN Other_Table AS Table_B
ON Table_A.id = Table_B.id WHERE
Table_A.col3 = 'cool'
this is working on sql server (microsoft). but cant work in oracle db. could you please tell me the reason ?
You may use merge into in Oracle.
MERGE Into Table_A t USING Table_B s
ON (t.id = s.id)
when matched then UPDATE SET
t.col1 = s.col1, t.col2 = s.col2
WHERE t.col3 = 'cool'
You can simply do this:
UPDATE table_a SET table_a.col1 = (SELECT table_B.COl1
FROM table_B
WHERE table_a.id = table_b.id),
table_a.col2 = (SELECT table_B.COl2
FROM table_B
WHERE table_a.id = table_b.id)
WHERE table_a.col3='cool';
Here is one approach which might work:
UPDATE
(
SELECT a.col1 AS col1a, a.col2 AS col2a, b.col1 AS col1b, b.col2 AS col2b
FROM Some_Table a
INNER JOIN Other_Table b
ON a.id = b.id
WHERE a.col3 = 'cool'
) t
SET
a.col1a = b.col1b,
a.col2a = b.col2b;
If Oracle doesn't want to run the above, then you will have to use correlated subqueries.

update column of table with other column

i hava table A
col1 col2
-------------
1 a
2 hhhh
3 erer
4 sdfsdfds
5 alimd
table a has relationshib other tables.
and other table is name B
col1 col2
----------------
1 hhjgjh
2 jkkjerwe
3 jjjjj
tables A , B have milions of records
question : i want to update col2 of table A with col2 of table B
the best and speed of query for update
thanks
UPDATE A SET A.col2 = B.col2
FROM TableA A INNER JOIN TableB B ON A.col1 = B.col1
Demo
update tableA
set tableA.col2 = tableB.col2
from tableB
where tableA.col1 = tableB.col1
This query will work:
update TableA
set col2 = b.col2
from TableA a
inner join TableB b on b.col1 = a.col1
UPDATE a SET a.col2 = b.col2 FROM a left join b on a.col1 = b.col1
update TabA
set col2 = b.col2
from TabB B
where b.col1 = a.col1

Updating a table with in clause Error

UPDATE
TABLE1 a,
TABLE2 b
SET
a.COL1 = 'VALUE'
WHERE
a.FK = b.PK
AND b.COL2 IN ('A subquery')
If I am using this update statement and the subquery in the IN clause does not return any rows, I get an error. How do I avoid that? (Oracle 10g)
You likely can rewrite this to an EXISTS query, depending on the exact details of your subquery:
UPDATE TABLE1 a, TABLE2 b SET a.COL1 = 'VALUE'
WHERE a.FK = b.PK AND EXISTS (select 1 from OTHERTABLE O where B.COL2=O.COL2)
UPDATE
TABLE1 a
SET
a.COL1 = 'VALUE'
WHERE
a.FK IN (SELECT b.PK FROM b WHERE b.COL2 in ....)
Is your subquery in quotes?
UPDATE
TABLE1 a,
TABLE2 b
SET
a.COL1 = value
WHERE
a.FK = b.PK
AND b.COL2 IN (
SELECT col2
FROM ...
WHERE ...
)

update table with join without PK in oracle

UPDATE
(
SELECT
a.COL1
FROM
TABLE1 a,
TABLE2 b,
TABLE3 c
WHERE
a.name = b.name
c.ccol = b.ccol AND
AND b.col1 = 'anyvalue'
AND a.col2 = 'anothervalue'
) u
SET
u.COL1 = 'VALUE'
This query does not work, since TABLE1 does not contains PK. How to write such a query ?
The following should achieve what it looks like you are trying to achieve above:
UPDATE TABLE1
SET COL1 = 'VALUE'
WHERE EXISTS
( SELECT 1
FROM TABLE2 B
INNER JOIN TABLE3 C
ON B.Ccol = C.Ccol
WHERE b.Name = Table1.Name
AND b.Col1 = 'AnyValue'
AND c.Col1 = 'AnotherValue'
)
I've never worked in oracle, but try something like this. Not having a pk shouldn't be an issue, as long as your joins & where statements are correct.
This is the SQL equivalent of what you're asking for
UPDATE u
SET u.COL1 = 'VALUE'
FROM Table1 AS u
INNER JOIN Table2 AS b ON u.name = b.name
INNER JOIN Table3 AS c ON c.ccol = b.ccol
WHERE b.col1 = 'anyvalue' AND u.col2 = 'anothervalue'