Issue with update query - sql

update t1
left join t2 on t1.val=t2.val
set t1.pid= pr.pid
from (select pid from t3) pr
where t1.acct='abc'
and case when t2.val is null then md5(some cols) = pr.md5(some cols)
This is my update query. My requirement is I need to update t1's pid with t3's pid value but i have filter some of the values (col name 'val') from t1 while performing an update. so i populate the values in to a new table t2 and did left join with t1.
Can some one help if my syntax is correct?

Related

Update row in a table based on multiple rows in another table

I have two tables: table1 and table2:
table1 has columns id and integer
table2 has columns id and boolean
table2 can have multiple rows with the same id
I want to update the integer column of table1 by looking at all rows with the same id in table2 and seeing if any of the boolean values are true. If so I want table1.integer to be 1, else I want it to be 0.
I have tried something like this:
UPDATE table1,
(
SELECT table2.id, Sum(table2.boolean) > 0
) AS 'condition'
from table2
WHERE 1
GROUP BY table2.id) table3
SET table1.integer =IF(table3.condition, 1, 0) where table1.id = table3.id
And it seems to work, but I wanted to ask if there is a nicer/cleaner/more succinct way of updating the rows of table1 according to multiple rows of table2.
I would recommend EXISTS:
UPDATE table1 t1
SET t1.integer = (EXISTS (SELECT 1
FROM table2 t2
WHERE t2.id = t.id AND
t2.boolean
)
);
This can take advantage of an index on table2(id, boolean). With such an index, it should be faster than an approach that uses JOIN and AGGREGATION.
The syntax of your query is MySql like, so you can do a join like this:
UPDATE table1 t1 INNER JOIN (
SELECT id, MAX(boolean) maxboolean
FROM table2
GROUP BY id
) t2 ON t2.id = t1.id
SET t1.integer = t2.maxboolean
If there are ids in table1 without a corresponding id in table2 and you want the integer column for them to be updated to 0 then use a LEFT join:
UPDATE table1 t1 LEFT JOIN (
SELECT id, MAX(boolean) maxboolean
FROM table2
GROUP BY id
) t2 ON t2.id = t1.id
SET t1.integer = COALESCE(t2.maxboolean, 0)

Move data from tb1 to tbl2 where

I need to move data from column reg in table1 to reg2 in table2, where the ID of table1 = 'ID2' of table2.
basically, I need to shift car registrations from A to B as long as the person exists in B.
update table column by using join
update t2
set t2.reg2=t1.reg
from table2 t2 inner join table1 t1
on t2.ID2=t1.ID
INSERT INTO table2 (reg2)
SELECT reg
FROM table1
WHERE table2.ID2 = table1.ID;

Update table column based on another table based on ID

I have 2 tables 'table1' and 'table2'.
table1 has 10,000 records and table2 has 5,000 records.
Both tables have "RECORD_ID" column. All RECORD_ID's that are in table2 can be found in table1.
I want to UPDATE the "PRICE" column of table1 based on the "RECORD_ID" column of table2.
update table1 set PRICE = table2.PRICE where RECORD_ID = table2.RECORD_ID
I got this error message:
SQL0206N "table2.PRICE" is not valid in the context where it is used
SQLSTATE=42703
I am using DB2.
UPDATE table1 SET table1.price = (SELECT table2.price FROM table2 WHERE table2.record_id = table1.record_id)
Try this:
UPDATE table1 f1
SET f1.price =(
SELECT f2.price
FROM table2 f2
WHERE f2.record_id = f1.record_id
)
WHERE exists
(
SELECT f2.price
FROM table2 f2
WHERE f2.record_id = f1.record_id
)
You have to use a join like this:
UPDATE
Table1
SET
Table1.Column = T2.Column
FROM
Table1 T1
INNER JOIN
Table2 T2
ON
T1.PK = T2.FK;

How to insert not matching existing row and also Update non Existing row

I Have two Table T1 and T2 of identical structure is as follows, ID and Filename Combination identifies a Unique ROW in both table
T1 T2
ID FileName IsActive ID FileName IsActive
Now I want to write two different queries in two different scenario
Scenario 1
Row exist T1 but not in T2
in this scenario insert the rows which exist in T1 but not in T2
Scenario 2
ROW Doest not exist in T1 but exist in T2
in this scenario update T2 set IsActive of that rows as 0
ID and Filename Combination identifies a Unique ROW in both table
Any help in writing the query?
First query:
INSERT INTO T2 (ID, FileName, IsActive)
SELECT T1.ID, T1.FileName, T1.IsActive
FROM T1 LEFT JOIN T2
ON T1.ID = T2.ID AND T1.FileName = T2.FileName
WHERE T2.ID IS NULL
Second query:
UPDATE T2
SET T2.IsActive = 0
FROM T2 LEFT JOIN T1
ON T2.ID = T1.ID AND T2.FileName = T1.FileName
WHERE T1.ID IS NULL

Update statement for multiple ids

I have 3 tables, i need to update 3rd table's column by calculating data from other two tables.
update table3 set column3=
(
select t2.column3+t1.column3
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id= 100
)
where id= 100;
This query works fine, it updates the 3rd table column, however if i supply IN operators like this:
update table3 set column3=
(
select t2.column3+t1.column3
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id IN (100,101)
)
where id IN (100,101);
this fails and i get this message
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
& i know this is because subquery is returning more than 1 row, how can i handle this scenario? Any hint/thought will be helpful.
How do i update for multiple ids? ie. the select query value returned by ID 100 should be updated against ID 100 in 3rd table & similarly for ID 101.
Also, I need to do a sum like this sum(t2.column3)- (t1.column3 + t1.column2)
update table3 set column3=
(
select sum(t2.column3)- (t1.column3 + t1.column2)
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id IN (100,101)
)
where id IN (100,101);
This is because you are trying to set column3 to a returned result, and SQL expects that to be one value only (scalar). The SQL engine gets confused when you pass it more than one return value (which one should it use?...it does not assume to iterate through the results). So, if you want to update an entire result set, then you need to create a subtable from you query and join on that. Your query should look more like this
UPDATE Table3
SET Column3 = subtable.value
FROM Table3
JOIN (
select t2.column3+t1.column3 as value, t1.id
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id IN (100,101)
) AS subtable
ON subtable.id = Table3.id
WHERE table3.id IN (100, 101)
Under this assumption that table3.id matches the other id's, you also really do not need the inner where table2.id IN ...
You should also join table3 in your UPDATE. Try this:
UPDATE t3
SET column3 = t2.column3+t1.column3
FROM table3 t3
INNER JOIN table2 t2 WITH(NOLOCK)
ON t3.id = t2.id
INNER JOIN table1 t1
ON t3.id=t1.id
WHERE t3.id IN (100,101)