SQL query to find EmployeeName, Manager Name from a table - sql

I have 2 tables as follows
EmployeeTable:
Employe_Manger:
Note that E1 is manager for E2, E3 and E4 are managers for E5.
Now find a query to find out the manager name and employee name side by side.
i.e. result should be

SELECT one.EmployeeName AS "Employee", two.EmployeeName AS "Manager"
FROM Employee AS one
INNER JOIN Employee_Manager AS temp ON one.EmployeeId = temp.EmployeeId
INNER JOIN Employee AS two ON temp.ManagerId = two.EmployeeId
Is it what you want to achieve?

Related

How to find levels of managers in self-referenced key

So I have a table employees(ID, MaganerID)
My task is to find 2-nd level manager who is not getting any bonuses(another table)
I'm stuck here. Any advice what should I do next?
SELECT ID
FROM Employees
WHERE ID NOT IN (Select EmployeeID FROM Bonuses)
AND ID IN (Select LeaderID FROM Employees WHERE LeaderID IS NOT NULL)
AND ID NOT IN (Select ID FROM Employees WHERE ID <> LeaderID)
Well since you want a level 2 manager is much more simple:
SELECT DISTINCT E2.ID
FROM Employees E
INNER JOIN Employees E1 ON E1.ID = E.ManagerID
INNER JOIN Employees E2 ON E2.ID = E1.ManagerID
INNER JOIN Bonuses B ON B.EmployeeID = E2.ID AND B.Bonus = 0
You could also use the Recurse option using the WITH keyword if you are using TSQL. If you simply dont want to not exists on the other table just reverse the last bool check on the join.
Presumably by second-level manager you mean where their EmployeeID is somebody else's LeaderID, whose EmployeeID in turn is a third person's LeaderID?
I think currently you'll just get first level managers.
This should work... I've included your final WHERE statement, although you don't explain what that's for. Do you really want to exlude records where the ID is different to the LeaderID?
SELECT L2.ID
FROM Employees E
JOIN Employees L1 on E.LeaderID = L1.ID
JOIN Employees L2 on L1.LeaderID = L2.ID
WHERE L2.ID not in (SELECT EmployeeID from Bonuses)
AND L2.ID NOT IN (Select ID FROM Employees WHERE ID <> LeaderID)

retrive sql query

SQL query to retrive the following condition. There are two cloumns one with user_id;s as primary keys and another tale as manager key. i want to retrive the query so that it displays manager key along with the user id's as manager 1, manager 2
Try this: you can achieve this using a self join
SELECT e1.name, e1.managerid, e2.name, e2.user_id
FROM Employee e1
INNER JOIN Employee e2
ON e1.managerid= e2.user_id
Try this, you can use the same approach for finding the heirarchy:
SELECT e1.name AS EmployeeName
,e1.user_id AS USERID
,e2.name AS ManagerName1
,e2.user_id AS ManagerID1
,e3.name AS ManagerName2
,e3.user_id AS ManagerID2
FROM Employee e1
LEFT JOIN Employee e2
ON e1.managerid = e2.user_id
LEFT JOIN Employee e3
ON e2.managerid = e3.user_id

SQL Server : replace several instances of userID with username

I may be overthinking this but I have not managed to figure it out or find a solution, so I'm hoping for a pointer in the right direction. I tried using the Select ColumnA AS Column B etc but it's not doing what I want.
I have 2 tables, scenario examples below
Table 1 (Vehicle)
VehicleID (001)
VehicleMake (Ford)
VehicleModel (Falcon)
VehicleExCleanEmpID (005)
VehicleIntCleanEmpID (003)
Table 2 (Employee)
EmpID (005)
EmpName (Dave)
The scenario being that a vehicle is cleaned internally or externally by any one of a pool of employees shown by the relevant ID in the Vehicles table.
I want to show in a query VehicleID, InsideCleanName, ExternalCleanName rather than showing the employee's ID.
So end up with results similar to this
VehicleID InsideCleanName ExternalCleanName
------------------------------------------------
001 Bob Dave
002 Sue Dave
003 John Sid
Thanks for any tips and or help
THat seems like a pretty simple query with two inner joins to the Employee table - something like this:
SELECT
v.VehicleId,
InsideCleanName = e1.EmpName,
ExternalCleanName = e2.EmpName
FROM
dbo.Vehicle v
INNER JOIN
dbo.Employee e1 ON v.VehicleIntCleanEmpId = e1.EmpID
INNER JOIN
dbo.Employee e2 ON v.VehicleExCleanEmpId = e2.EmpID
Joining to the Employee e1 table is giving you the employee who was responsible for the inside cleaning, while joining a second time, to Employee e2 gives you the one responsible for the external cleaning.
Join the two table with EmpId and select the columns you want similar to the code below:
select column1, column2 from table1 inner join table2 on table1.EmpId = table2.EmpId

SQL subquery Total/Count

I am trying to write a query that lists the name of a manager and the number of people they manage.
In the Manager table we have the managers name and id.
In the Employee table we have the employees name, id and managerID.
I don't understand how to get the count of the employees that a manager manages.
SELECT COUNT(e.EmpID), m.ManagerID
FROM Employee e
INNER JOIN Manager m
ON e.ManagerID= m.ManagerID
GROUP BY m.ManagerID
SELECT m.Name, COUNT(e.id) AS NumberOfEmployeesManaged
FROM Manager m INNER JOIN Employee e ON m.id = e.managerID
GROUP BY m.Name
That should do it I think, just a simple count of the employee ids after joining the manager and employee tables, grouped on manager name.
SELECT count(emp.empid), mgr.managerid
FROM Employee emp
INNER JOIN Manager mgr ON emp.managerid=mgr.managerid
GROUP BY mgr.managerid;
I don't know if you can use the COUNT aggregator in a JOIN. But you can run 2 queries. One would select the manager's name & id. The 2nd would look like this:
$id = the manager's id
SELECT COUNT(*) FROM Employee WHERE managerID=$id
Alternately, you could not use COUNT and run a query like this:
SELECT id FROM Employee WHERE managerID=$id
Then the # of resulting rows would be the count of employees managed by the manager.

Combining tables in SQL/QlikView

Is it possible to combine 2 tables with a join or similar construct so that all non matching field in one group. Some thing like this:
All employees with a department name gets their real department and all with no department ends up in group "Other".
Department:
SectionDesc ID
Dep1 500
Dep2 501
Employee:
Name ID
Anders 500
Erik 501
root 0
Output:
Anders Dep1
Erik Dep2
root Other
Best Regards Anders Olme
What you are looking for is an outer join:
SELECT e.name, d.name
FROM employee e
LEFT OUTER JOIN departments d ON e.deptid = d.deptid
This would give you a d.name of NULL for every employee without a department. You can change this to 'Other' with something like this:
CASE WHEN d.name IS NULL THEN 'Other' Else d.name END
(Other, simpler versions for different DBMSs exist, but this should work for most.)
QlikView is a bit tricky, as all joins in QlikView are inner joins by default. There is some discussion in the online help about the different joins, short version is that you can create a new table based on different joins in the script that reads in your data. So you could have something like this in your script:
Emps: SELECT * FROM EMPLOYEES;
Deps: SELECT * FROM DEPARTMENTS;
/* or however else you get your data into QlikView */
EmpDep:
SELECT Emps.name, Deps.name
FROM EMPS LEFT JOIN Deps
In order for this join to work the column names for the join have to be the same in both tables. (If necessary, you can construct new columns for the join when loading the base tables.)