I am trying to join 3 tables to include only those employees who are assigned to more than one assignment. Through troubleshooting, I have found that my query works until I add my third table. Specifically, until I add the third table's columns to group by. I need the third table added, as it included the start and end date of the projects.
I have been at this for days. I have tried string_agg to bypass using the columns from the third table in the group by, but found that it it not supported in the program we're required to use. I have tried different joins, different counts, using variables. I have tried joining the tables in different combinations and subsequently worked through other errors. I have tried to do nested select statements.
At this point, I have lost count. I started out with a cartesian product and have now worked out that I think the problem lies somewhere with grouping third(projects) table.
CREATE VIEW Assignments AS
SELECT a.EmpIDNumber,
COUNT(*) AS Frequency,
e.FirstName,
e.LastName,
a.PositionOnProject,
P.AssignedToProject AS ProjectName,
P.ProjectBeginDate AS StartDate,
P.FinishOnProject AS End_Date
FROM Assignments a
INNER JOIN Employees e ON a.EmpIDNumber = e.EmpIDNumber
INNER JOIN Projects p ON a.projectID = p.ProjectID
GROUP BY e.FirstName,
E.LastName,
a.PositionOnProject,
a.EmpIDNumber,
p.AssignedToProject,
p.ProjectBeginDate,
p.FinishOnProject
HAVING COUNT(*) > 1;
As is, I get no results. Just empty headers. Removing the third table(projects) from the select and group by, I get the expected results, showing those employees who are assigned to more than one project.
I know I am doing something wrong and am open and eager to learn what that is. Any help in doing that is appreciated.
Employeestable though, and grouping on those.StartDate and End_DateI think it didn't happen on purpose but try to keep it the same :)