Update table according to the another table value - sql

I have two tables Employees
EmpID | EmpName | EmpDob
and WareHouseEmployeers
WarehouseEmpID | position | province
I need to update the Employee table according to the WareHouseEmployers table's values. How do I update the details about the employee table according to warehouse province and position?
I have tried this but it's not working:
UPDATE Employee
SET a.EmpName = 'Steven', a.EmpDob = '5-5-1990'
FROM Employee a, WareHouseEmployee b,
WHERE
a.EmpID = b.WareHouseEmpID
AND position = 'manager', province = 'central'
Can someone please help me to do that in SQL Server?

Please use this script
UPDATE a
SET a.EmpName = 'Steven' , a.EmpDob='5-5-1990'
FROM Employee a
INNER JOIN WareHouseEmployee b ON a.EmpID = b.WareHouseEmpID
AND position = 'manager'
AND province = 'central'

Related

finding a value in multi-values column

I have 2 tables as following:
Tam trying to get the department name of each employee (DepName column in table Emp table) from Dep table:
I have written this query:
update Emp
set DepName= (
select DepName
from Dep
where array_to_string(EmpID, ',') like EmpID
);
It did not update the table Emp with the requested information, although I haven't got any error. Any help?
You can do:
update emp
set dept = d.depname
from dep
where emp.empid = any (dep.empid);
Having pointed that out, you should not do this. Instead, I would suggest that you have a proper link to the department table and use join to bring in the department name.
you have to convert id int to character varying array data type and then use contains operator with table dept and update as usual
UPDATE emp t1
SET dept = dname
from dept t2
where t2.eid #> concat(concat('{',(t1.id::text)),'}') ::varchar[]
https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=e5c24b26b3479b68adf0b17c2050f715

does anyone know how to list the empid and the name of all supervisors if supervisors are in employee table too?

I have a table that contains empid, name, salary, hiredate, position and supervisor (which includes empid, not the name). How do I list the empid and name of all supervisors?
The output has to have to columns supervisor (and a list of their empid) and their names. This is the create statement used to create the Employee table:
/* Create table Employee */
IF OBJECT_ID('Employee', 'U') IS NOT NULL
DROP TABLE Employee
GO
CREATE TABLE Employee (
emp_id NCHAR(5),
name NVARCHAR(20),
position NVARCHAR(20),
hire_date DATETIME,
salary MONEY,
bcode NCHAR(3),
supervisor NCHAR(5)
)
I have tried a variety of statements using having statement and count but they don't seem to work.
select emp_id, name from employee where position='manager';
I tried this but it doesn't work. Anyone smart that knows how to do it?
You will have to join the table back on itself:
select a.name, a.position, a.hiredate, a.salary, a.supervisorid,
isnull(b.name, '') as SupervisorName
from EmployeeTable a
left join EmployeeTable b
on a.SupservisorID=b.ID
The left join will make sure that the employees who do not have supervisors are returned, and isnull(b.name, '<NONE>') can be used if you would like to have something other than NULL as a value in those cases.
SELECT e.empid ,ISNULL(b.name, 'No supervisor') SupervisorName
FROM employee e LEFT JOIN employee b
ON e.supervisorid = b.empid
Inner join will leave out the people who do not have a supervisor , Use left join to get all the employees
If you want supervisors only, you just need to select rows whose emp_id values are found in the supervisor column:
SELECT
SupervisorID = emp_id,
SupervisorName = name
FROM dbo.Employee
WHERE emp_id IN (SELECT supervisor FROM dbo.Employee)
;

How do I simplify this query?

so my main goal with this query is to select all departments and employees under a manager named Mario Souza.
SELECT d.deptname,e.name FROM employee e JOIN department d ON e.dept = d.deptnum WHERE idmanager IN(
SELECT id FROM employee WHERE name = 'Mario Souza'
) AND manager IN(
SELECT id FROM employee WHERE name = 'Mario Souza'
)
And it's working, but is there a way I could store the first IN result so I could use it later after the AND operator?
You can use EXISTS to match on multiple columns.
WHERE EXISTS
(
SELECT *
FROM employee AS manager
WHERE manager.name = 'Mario Souza'
AND manager.id = e.idmanager
AND manager.id = d.manager
)
You could use a JOIN with employee table. Simply put both manager and idmanager in ON clause.

SQL update in single query

I have 2 tables: employee and employeedetails.
employee looks like this:
id name
----------------
1 Suresh
2 Ram
3 Ravi
employeedetails looks like this:
empid salary
----------------
1 10000
2 5000
3 40000
I want to update salary field of a particular person. For this i know the employee name of the first table. Based on this, I need to write a single query that will update the salary field with single query. How can I do this?
For example, if I have to update Ravi's salary details, how can I do it in a single query?
update employeedetails
inner join employee on employeedetails.empid = employee.id
set salary = 1000
where employee.name = 'Ram'
Try this :
update employeedetails set salary=1000000 where empid in (select id from employee where name='suresh');
MERGE INTO employeedetails
USING employee
ON employeedetails.empid = employee.id
AND employee.name = 'Ravi'
WHEN MATCHED THEN
UPDATE
SET salary = 10000;
UPDATE employeedetails det
SET salary=100
WHERE EXISTS ( SELECT NULL
FROM employee emp
WHERE name='Ravi'
AND det.empid = emp.id
)
;

New Sql query solution

person_id | manager_id | name |
| | |
-------------------------------
I have to display name of every person with manager name.
Yes its complete table. Thats all I have.
This one should give you all employees that have a manager, with employee_name and manager_name. Replace your_table by your table name.
If you want to get all persons, also that without manager, replace the JOIN by a LEFT JOIN. This would return NULL as manager_name for all persons without manager_id.
SELECT t1.name employee_name, t2.name manager_name
FROM [your_table] t1
JOIN [your_table] t2 ON ( t1.manager_id = t2.person_id )
Which SQL dialect? Here's some TSQL, but I'm vague to the actual question ("every person with manager name"); if you mean "given a manager name, list the people (reports)", then:
SELECT peon.[person_id], peon.[name]
FROM [thetable] mgr
INNER JOIN [thetable] peon
ON peon.manager_id = mgr.[person_id]
WHERE mgr.[name] = #name
ORDER BY peon.[name]
If you mean "list the people, along with their manager's name", then:
SELECT peon.[person_id], peon.[name], mgr.[name] AS [manager]
FROM [thetable] peon
LEFT OUTER JOIN [thetable] mgr
ON mgr.[person_id] = peon.manager_id
ORDER BY peon.[name]
SELECT person.name, manager.name
FROM table person, table manager
WHERE person.manager_id = manager.person_id