Table with multiple columns containing employee keys. How to link to employee master to get names? - sql

My table has columns of data that contain employee keys for different departments. One column, for example would be business office, another admin.
I can't figure out how to write and sql that will join the employee master to return each persons name for each row. I could do it if it only had one column of employee master keys but it beat me

I think from what you have described you need to join to the Employee Master table multiple times:
SELECT A.Col1
, A.BOEmpID
, BO.EmpName
, A.AdminEmpID
, AD.EmpName
, <....>
FROM MyTable A
INNER JOIN Employees BO
ON A.BOEmpID = BO.EmpID
INNER JOIN Employees AD
ON A.AdminEmpID = AD.EmpID

Related

Selecting two names based on ID stored in different table

Suppose I have two tables:
Table 1: tasks with columns (id, authorId, assigneeId)
Table 2: employee with columns (id, name)
authorId and assigneeId in Table 1 are referenced to the id column in Table 2.
What is the way to select Task ID, Author Name and Assignee Name (Assignee name defaults to 'null' if no assigneeId is present) from these two tables?
I am not sure how to start, thus making it difficult. Could anyone give a hint on how to start the code?
You can join twice:
select t.id, e1.name as author_name, e2.name as assignee_name
from tasks t
left join employees e1 on e1.id = t.author_id
left join employees e2 on e2.id = t.assignee_id

Microsoft SMSS - Joining tables on t2.primary key = t1.foreign_key

I need to join two tables together to create a table with columns for employee id, employee name and their boss' name.
The 'hier' table
The 'employees' table
The query I wrote is almost working, putting an employee name in the right spot, but not the right employee:
SELECT em.emp_id, em.emp_name, em.emp_name AS boss_name
FROM employees em
LEFT JOIN hier h ON (h.boss_id = em.emp_name)
Which outputs:
I need to have each person's boss to have the right name, and in the case of Big Boss, 'N/A'. Like so:
You need a self join with Employee table
SELECT em.emp_id, em.emp_name, e1.emp_name AS boss_name
FROM employees em
LEFT JOIN employees em1 ON em.boss_id = em1.emp_id

Condition for finding rows with same value in one colum and null in another column

I have a table with columns student ID, subject, Enrolled. I have a row for each subject against a student. I am inserting this data to a temp table for reporting. I have another column named reporting in this temp table. I need to update reporting false when enrolled is null for all the rows imported for a student.
Here is the approach:
Select from all students
Left outer join to student-course (outer join is important here)
Group by student ID, and get min course ID
Use rows with null course ID to decide what temp rows to update
In SQL this query would look like this:
SELECT StudentId, MIN(CourseId)
FROM Student s
LEFT OUTER JOIN StudentCourse sc
ON s.StudentId=sc.StudentId
GROUP BY s.StudentId
HAVING MIN(CourseId) IS NULL

Insert primary keys from two newly created tables in a relationship table

I have one table which contained 4 records (PersonName, CityName, CityState, CityCountry) in two different tables. One of the tables now has personID, personName and the other one has Cityid, CityName, CityState, CityCountry.
Now I created third table which hold PersonId, CityId. How can I populate that table with the ids of person and city from the original table since they are split now. I want to get the ids from the newly created tables based on the relationship they had in the original table.
Can you not just join back to the original table?
INSERT PersonCity (PersonID, CityID)
SELECT p.PersonID, c.CityID
FROM OriginalTable o
INNER JOIN Person p
ON p.PersonName = o.Personname
INNER JOIN City c
ON c.CityName = o.CityName
AND c.CityState = o.CityState
AND c.CityCountry = o.CityCountry;

How to retrieve records having many to many relationship bet two tables?

I have two tables:
1) employee with columns: e_id, e_name, address;
2) project with columns: p_id, p_name, start_Date, End_Date.
There is a many-to-many relationship between these two tables.
How can I store this relationship and how can I query for retrieving "Employee details and project details on which he is working"?
You need a cross reference table inbetween:
EmployeeProjectXref: e_id, p_id
The combination of e_id and p_id can be the primary key for this Xref table.
Then, if #e_id is a variable holding the selected Employee ID:
SELECT E.e_name, P.p_name
FROM EmployeeProjectXref EPX
JOIN Employee E ON E.e_id = EPX.e_id
JOIN Project P ON P.p_id = EPX.p_id
WHERE EPX.e_id = #e_id
ORDER BY P.Name
For more details, add them to the SELECT list. E.g.: SELECT E.e_name, E.address, P.p_name, P.start_date, P.end_date, etc. Here the E is an alias for the Employee table, and P is an alias for the Project table. This query will return one row for each entry in the EmployeeProjectXref table (provided that it references real entries in the Employee and Project tables.) If there is one employee and three projects, you'll get three rows.