MS Sql - Update table 1 based on table 2 and table 3 - sql

I want to update table 1 based on table 2 and table 3. Right now am updating it in two separate statements.
Is it possible to update table 1 first based from table 2 then for the NULLS to be updated from table 3 all in a single update using join.
I have tried to build a query but where to include
tbl1.col1=tbl3.col1
update tbl1
set tbl1.col1= tbl2.col1
from table1 tbl1
left JOIN table2 tbl2
ON tbl1.col = tbl2.col
left JOIN table3 tbl3
on tbl1.col=tbl3.col and tbl2.col<>tbl3.col

I think you just want coalesce():
update tbl1
set col1 = coalesce(tbl2.col1, tbl3.col1)
from table1 tbl1 left join
table2 tbl2
on tbl1.col = tbl2.col left join
table3 tbl3
on tbl1.col = tbl3.col and tbl2.col <> tbl3.col;

Use COALESCE or ISNULL both returns the first NOT NULL value
Try this
UPDATE tbl1
SET tbl1.col1 = COALESCE(tbl2.col1, tbl3.col1) -- ISNULL(tbl2.col1, tbl3.col1)
FROM table1 tbl1
LEFT JOIN table2 tbl2
ON tbl1.col = tbl2.col
LEFT JOIN table3 tbl3
ON tbl1.col = tbl3.col
AND tbl2.col <> tbl3.col

Related

MSSQL Insert values from table2 into table1 using condition from table2

I have two tables: table1 and table2 I need to migrate some data from table2 into table1 by matching or using a condition from both tables.
What I need is something like:
INSERT INTO Tbl1(VAR1,VAR2)
SELECT VAR1, VAR2
FROM Tbl2
WHERE Tbl1.Name = Tbl2.Name and Tbl1.City = Tbl2.City
I have a sample schema here
It seems you need update not insert
update Tbl1
set Tbl1.VAR1= Tbl2.VAR1,
Tbl1.VAR2= Tbl2.VAR2
FROM Tbl2 join Tbl1
on Tbl1.Name = Tbl2.Name and Tbl1.ADDRESS = Tbl2.ADDRESS
one line demo
You could use a inner join for select the right result from both the tables
INSERT INTO Tbl1(VAR1,VAR2)
SELECT VAR1, VAR2
FROM Tbl2
INNER JOIN tbl1 ON Tbl1.Name = Tbl2.Name
and Tbl1.City = Tbl2.City

INSERT INTO ... SELECT with more SELECT columns

Thank you for your time.
I have a INSERT INTO ... SELECT setup, but I also want to filter using extra columns (that do not exist in the table I am inserting into)
For example:
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3,
t4.filter_col
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
table1 only has columns t1_col1, t1_col2 and t1_col3 so when I attempt to run this it fails as expected:
ERROR: INSERT has more expressions than target columns
So my question is, how can I still include the filter, but specify which columns from my SELECT statement should be used in the INSERT INTO statement.
Many thanks for any help!
you have to same number of column in select ,so remove t4.filter_col from selection
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';
Note: you are not bound to select all columns those you used in join or filter
It's almost right, but you select one column too much. Try this:
INSERT INTO table1 (t1_col1, t1_col2, t1_col3)
SELECT
t2.t1_col1,
t2.t1_col2,
t3.t1_col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.t1_col1 = t3.t1_col1
INNER JOIN table4 t4 ON t4.filter_col = t2.filer_col
WHERE t4.filter_col = 'value';

Update multiple rows in SQL Server with different table

I have two tables let's say tables A and B. Each table has two columns ID and Name but the data in column Name of table A and B is different, but ID are the same, so I want to update column Name in table A with values from column Name in table B. How to achieve this? If any could be helpful
update tbl1 set tbl1.Name = tbl2.Name
from Table1 as tbl1
inner join Table2 as tbl2 on tbl1.Id = tbl2.Id
this is what your are looking for
Try with UPDATE using JOIN
UPDATE TableA
SET TableA.Name= TableB.Name
FROM TableA INNER JOIN TableB
ON TableA.ID= TableB.ID
Here you can do as join
UPDATE table_a
SET table_a.Name = table_b.Name
FROM table_a
INNER JOIN table_b ON table_a.id = table_b.id

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 table1 from table2 value by recent date

I wanted to update table1 based on table2 which has a common field employee_ID. I always have unique employee_ID in table1 but in table2 it may contain duplicate records with same employee_ID.
I got another column in table2 where the modified_date is inserted. I want to update the table1 employee name with the table2 employee name based on employee_id and recent modified date.
I got multiple columns to update with the same type of condition. any idea, here is so far i tried.
Here is the query, i have using inner join,
ssql = "Update Table1 INNER JOIN Table2
ON Table1.employee_id= Table2.employee_id
SET Table1.type= Table2.type"
any help will be appreciated
try this query
update t1 set t1.employee_name = t2.employee_name from table1 as t1
inner join table2 as t2 on t1.employee_id = t2.employee_id
where t2.Modified_date = (select Max(modified_date) from table2 as tb2 where
tb2.employee_id = t2.employee_id group by tb2.employee_id)
You need an intermediate step to associate the Max(modified_date) to each employee_id.
Using a CTE expression you can try something like this :
with
more_recent as (
select
employee_id,
Max(modified_date) max_modified_date
from
table2
group by
employee_id
)
update
t1
set
t1.employee_name = t2.employee_name
from
table1 as t1
inner join more_recent mr on
t1.employee_id = mr.employee_id
inner join table2 as t2 on
mr.employee_id = t2.employee_id
mr.max_modified_date = t2.modified_date