Updating values in table2 using table1 - sql

I am having two tables, table1 having three columns (id,names,salary) and table2 having three columns(id,names,totalsalary). I want to update table2 totalsalary column by using table1. Please, find the below attachment of tables in images

You could do inner join both tables and update totalsalary column on table2 using salary column from table1
update t2
set t2.totalsalary = t1.salary * 12 (Some calculation if needed)
from table2 t2
inner join table1 t1 on t1.id = t2.id

It seems you want to UPDATE the salary value in table2 with the sum of the salaries in table1. The most straight forward way to do this is with a sub-query and a JOIN.
The sub-query:
SELECT
id,
SUM(salary) as TotalSalary
FROM
table1;
And then the JOIN:
UPDATE t2
SET
salary = t1.TotalSalary
FROM
table2 AS t2
JOIN
(SELECT
id,
SUM(salary) as TotalSalary
FROM
table1
) AS t1
ON t1.id = t2.id;

Related

How do I only import unique records into a table from another table?

I am trying to update a table in SQL Server. Table is called table1 with columns(code, desc). I want to update table1 with unique records from table2(code, desc). table2 is a copy of table1 but it contains records that are not present in table1.
EDIT 1: table1 consists of all the records in our ERP when exported in July, table2 consists of all the records in our ERP when exported in November (including records added after July which are the records that I want to add to table1)
To update table1.desc with values from matching rows in table2 simply do:
update t1 set
t1.desc = t2.desc
from table1 t1
join table2 t2 on t2.code = t1.code;
If however you want to insert rows into table1 that only exist in table2 (it's not exactly clear if that's the case) you can use not exists
insert into table1 (code, desc)
select code, desc
from table2 t2
where not exists (select * from table1 t1 where t1.code = t2.code);
Sounds like you want an INSERT
Something like this should work:
INSERT INTO table1 (code, desc)
SELECT t2.code, t2.desc
FROM table2 t2
LEFT JOIN table1 t1 on t2.code = t1.code and t1.desc = t2.desc
WHERE t1.code is null --Ignore records that already exist in table1
... Adjust join clause accordingly.

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

update statement using in equijoin

I have two tables TABLE1 contains columns (deptno,sal) and TABLE2 contains columns (deptno and location) now I want to update 1000 more salary in the sal column for those persons that are working in PARIS(location column table 2) could you please answer me how I can do the update?
Try this:
UPDATE t1 SET t1.sal = t1.sal+1000
From Table1 as t1 INNER JOIN Table2 as t2 On t1.deptno = t2.deptno
WHERE t2.location = 'PARIS'

SQL Insert Data If Column A Matches and Column B Doesn't

I have a SQL Insert statement that needs to insert records into another table only if the the record doesn't exist in table2 or the zip code has changes in table1. I have tried the following but it throws an error and it is the logic I am looking for:
INSERT INTO table2
SELECT id, zip
FROM table1 t1
JOIN table2 t2
ON t1.id = t2.id and t1.zip <> t2.zip
I also need it to insert the records if the id doesn't exist at all in table2. I have googled the crap out of this and can't seem to find the solution anywhere.
What about this?
INSERT INTO table2
SELECT t2.id, t2.zip
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.id = t2.id
WHERE (t1.id IS NULL OR t2.zip <> t1.zip)
Also, be sure to clarify which table's id and zip columns you are asking for.
You should always include column lists when doing inserts. Second, your query doesn't quite capture your logic. You need a left outer join to find the records that don't exist in the second table. Perhaps this might do what you want:
INSERT INTO table2(id, zip)
SELECT id, zip
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.id = t2.id
WHERE (t1.zip <> t2.zip) or (t2.zip is null)
You just need a WHERE NOT EXISTS clause
INSERT INTO table2
SELECT id, zip
FROM table1
WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table2.id = table1.id AND table2.zip = table1.zip)

query in sql server 2005

there are two tables ...know i need
1st condition:
all the records in table
2 nd condition:
In table2 i need only records which have data
...i want one query for the aove two conditions...
SELECT
*
FROM Table1 t1
INNER JOIN Table2 t2 on t1.PK = t2.FK
This will return all rows in table1 that have at least one corresponding row in table2
But if you want all rows from t1 no matter what then this may be what you want
SELECT
*
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.PK = t2.FK
Finally, As I dont know the structure in place perhaps table1 and table2 have similar structures. If this is true perhaps you may want a union of the two
SELECT
*
FROM Table1 t1
UNION ALL
SELECT
*
FROM Table2 t2