update column of table with other column - sql

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

Related

Filter out entire records SQL

I have two tables: TableA and TableB
TableA Has 10 records while TableB has 8 Records.
I am trying to filter the distinct records on table B from TableA so I can then move the discrepancy to TableB.
This is a Legacy, poorly made, Database, so there is no Unique Identifiers. So they look something like this.
TableA TableB
Col1, Col2, Col3 Col1,Col2,Col3
1 X X Y X X Y
2 X Y Y X Y Y
3 X X X
I want to filter the combination of values for each record on TableB to find the missing values that are present on TableA
Use NOT EXIST
select *
from tablea ta
where NOT EXIST (
select 1
from tableb tb
where ta.col1 = tb.col1
and ta.col2 = tb.col2
and ta.col3 = tb.col3)
You need a left join from tablea to tableb and get only the rows of tablea that do not match:
select a.*
from tablea a left join tableb b
on b.col1 = a.col1 and b.col2 = a.col2 and b.col3 = a.col3
where b.col1 is null and b.col2 is null and b.col3 is null

Left Join Table with Exclusive OR

Is there a way to join 2 tables together on one and only one of the possible conditions? Joining on condition "a" or "b" could duplicate rows, but I'm looking to only join once. I came up with a potential solution, but I'm wondering if there is a more slick way to do it.
For example:
SELECT *
FROM TableA a
LEFT JOIN TableB b
ON a.col1 = b.col1
OR (a.col1 != b.col1 AND a.col2 = b.col2)
This would join the tables on col1 OR col2 BUT NOT BOTH. Is there a cleaner way of doing this?
Not more efficient but I think more clear
SELECT *
FROM TableA a
LEFT JOIN TableB b
ON (a.col1 = b.col1 or a.col2 = b.col2)
AND NOT (a.col1 = b.col1 and a.col2 = b.col2)
Your method works. If you only want one (or a handful) of columns from b, I would suggest:
SELECT a.*, COALESCE(b.col3, b2.col3)
FROM TableA a LEFT JOIN
TableB b
ON a.col1 = b.col1 LEFT JOIN
TableB b2
ON a.col1 <> b2.col1 AND a.col2 = b2.col2;
Removing the OR from the JOIN conditions allows the optimizer to generate a better execution plan.

Oracle DELETE sql with JOIN doesn't work

My delete statement returns a 933 error in Oracle, I'm not sure what is wrong-
DELETE b
from temp a
JOIN
fact_tab b
on a.col1 = b.col1
and a.col2 = b.col2
and a.col3 = b.col3;
Both tables dont have a primary key. select statement on the same thing works-
select *
from temp a
JOIN
fact_tab b
on a.col1 = b.col1
and a.col2 = b.col2
and a.col3 = b.col3;
try this
DELETE FROM
fact_tab
WHERE
EXISTS
(
SELECT
1
FROM
temp
WHERE
temp.col1 = fact_tab.col1 AND
temp.col2 = fact_tab.col2 AND
temp.col2 = fact_tab.col2
)
Oracle doesn't allow JOIN in a DELETE statement directly like that.
You could do the delete in the following way:
DELETE
FROM fact_tab
WHERE ROWID IN
(SELECT b.rowid
FROM temp a
JOIN fact_tab b
ON a.col1 = b.col1
AND A.col2 = b.col2
AND A.col3 = b.col3
);
You could also use WHERE EXISTS clause.

Netezza UPDATE from one table to another

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;

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 ...
)