Update table property from a value in a same table - sql

I have an Employee Data table with following properties
Id Emp_name Supervisor_Name Supervisor_id
1 A B
2 B
3 C B
I want to update the Supervisor_Id column with Id value equal to Supervisor_Name. The output will be as follows.
Id Emp_name Supervisor_Name Supervisor_id
1 A B 2
2 B
3 C B 2
What is the optimum SQL query to do this

Self join would do the job
update e1
set e1.Supervisor_id=e.id
from Employee e1
join Employee e
on e1.Supervisor_Name =e.Emp_name

Use UPDATE with INNER JOIN syntax:
UPDATE t1
SET t1.Supervisor_id = t2.Id
FROM mytable t1
INNER JOIN mytable t2 ON t1.Supervisor_Name = t2.Emp_name
Demo here

Related

How to get Oracle to return unique results in a one to many relationship tables with a left join

I have a three tables
Table 1
Id Department
1 A
2 B
3 C
4 D
Table 2
Id DepartId Name
1 1 ABC
2 1 DEF
3 1 ASD
4 2 FGH
5 2 HJK
6 3 ZXC
Table 3
Id Depart Area
1 A pp
2 B
3 C nn
4 D oo
I need the result
Id Depart Name Area
1 A ABC pp
2 B FGH Null
3 C ZXC nn
4 D NULL oo
I need one matching entry from table 2 and table 3 to corresponding entry in the table 1
Do a left join to also get t1 rows without any reference in the t2 table. GROUP BY to get only 1 row per Department.
select t1.id, t1.Department, min(t2.Name)
from t1
left join t2 on t1.id = t2.DepartId
group by t1.id, t1.Department
I think I would do this with a correlated subquery:
select t1.*,
(select t2.name
from t2
where t1.id = t2.DepartId and rownum = 1
) as t2name
from t1;
This saves the overhead of an aggregation. An index on t2(DepartId, name) is optimal for this query.
by the way not the answer to your specific question but if instead of just one you want all the names you can use listagg
SELECT t1.id,
department,
LISTAGG (name, ',') WITHIN GROUP (ORDER BY name) names
FROM t1, t2
WHERE t1.id = t2.departId(+)
GROUP BY t1.id, department
ORDER BY 1
ID Department Names
1 A ABC,ASD,DEF
2 B FGH, HJK
3 C ZXC
4 D

SQL query for getting the value ,based on a condition

Hi I need a help to get one sql Query. I have one table 'EMP' .
id name mid
1 A 1
2 B 1
3 C 2
4 D 1
5 E 2
I want the result as
id name mid
1 A A
2 B A
3 C B
4 D A
5 E B
can any one help me to do this.
It is a simple self join:
select id, t1.name, t2.name from emp t1 join emp t2 on t1.mid=t2.id
using inner join:
select EMP1.id, EMP1.name, EMP2.name AS MID from EMP AS EMP1 INNER JOIN EMP AS EMP2 on EMP1.mid=EMP2.id
ORDER BY EMP1.ID
Beside #alex answer you can also try these two too:
Method 1
SELECT T.Id ,T.Name ,D.Name AS mid
FROM EMP AS D
CROSS APPLY (SELECT Id ,Name FROM EMP AS T WHERE mid = D.id) AS T;
Method 2
SELECT Id ,Name ,(SELECT Name FROM EMP WHERE id = T.mid) AS mid
FROM EMP AS T;

Inner join - more than one table

I'm trying to write a code that will join together information from 3 tables and compare it with a "big table" and according to the comparison update a field in a test table. Look at the example:
Big Table:
Employee_Id status
2322 5
222 3
545 6
4532 2
Table 1:
S_Id status
2322 7
Table 2:
S_Id status
222 3
Table 3:
S_Id status
545 6
4532 3
Test Table:
TE_Id IsNotGood
2322 1
4532 1
222 0
545 0
The id "2322" got 1 because the status in table 1 is not like the status in the big table and same for 4532.
I started writing this:
update Test Table
set isNotGood = 0
with ids as (
select distinct Employee_Id from (
select Employee_Id,Status from BigTable as B
) as T
inner join table1 as W on W.S_ID = T.Employee_Id
inner join table2 as K on K.S_ID = T.Employee_Id
inner join table3 as R on R.S_ID = T.Employee_Id
)
I would be happy for your tips to finish this query.
Thank you very much!
You won't be able to use an inner join unless the employee_id is in all the tables.
UPDATE Test
SET isNotGood = 1
WHERE te_id in (select employee_id
from [Big Table] bt
left join table1 as W on W.s_id = bt.employee_Id
left join table2 as K on K.s_id = bt.employee_Id
left join table3 as R on R.s_id = bt.employee_Id
where (bt.status <> W.status and W.status is not null) or (bt.status <> K.status and k.status is not null) or (bt.status <> R.status and R.status is not null);

Update table row with data from another table

I'm quite new to sql and i need your help to solve this problem. I have 2 tables. What I want is to update a row from one table with data from another one but only if the values are different where the id is the same.
Like this:
Table A
ID DESC
1 asd
2 aaa
3 asda
Table B
ID DESC
1 asd33
2 aaa22
3 asda
And what I want is this update DESC table B with data from DESC A only if the values are different
Table B
ID DESC
1 asd
2 aaa
3 asda
UPDATE B
SET B.DESC = A.DESC
FROM TABLEB B
LEFT JOIN TABLEA A on A.ID = B.ID
Try this
Update TableB
Set TableB.desc = TableA.desc
From TableB INNER JOIN TableA ON TableB.ID = TableA.ID
Where TableB.desc NOT IN
(Select ISNULL(TableA.desc,'') From TableA)

how to select distinct values from two tables limit to one row

I have two tables with this values:
**table 1**
id name value
1 A 1
2 B 1
3 F 2
4 G 3
**table 2**
id name value
1 C 1
2 D 1
3 E 2
If I do an inner join of both tables by the value I´m getting this:
A C
A D
B C
B D
F E
But, the problem is that I want only distinct values from both columns like that:
A C
B D
F E
Another set of posible result will be:
A D
B C
F E
There´s no chance of name of table 1 will appear in table 2.
If one value from a column was already selected, it can´t be selected again. This example will be an error because C was already selected:
A C
B C
F E
Any ideas?
In order to pair the records, you need a running number per value to link with. Use row_number() for this.
select t1.name as t1_name, t2.name as t2_name
from
(
select name, value, row_number() over (partition by value order by name) as rn
from table1
) t1
join
(
select name, value, row_number() over (partition by value order by name) as rn
from table2
) t2
on t1.value = t2.value and t1.rn = t2.rn;
SQL fiddle: http://www.sqlfiddle.com/#!4/75de0/1.
Based on the data you provided, and the column you're using to join, you're getting the results you should be getting.
To get your desired results, you would need to join on id, not value
as such:
select a.id, a.name, b.name
from tableA a
inner join tableB b on a.id = b.id