I have the following database design:
Employee Table: Username, Name, DivisionCode
Division Table: SapCode, DivisionShortcut
Courses Table: CourseID, CourseName, GroupID
Groups Table: GroupID, GroupName
Employee_Courses Table: EmployeeID, CourseID
I came up with the following query that shows the divisions, courses and the total number of participants in each division:
SELECT dbo.Divisions.DivisionShortcut, dbo.courses.CourseName, COUNT(dbo.employee_courses.courseId) AS [Total Number of Participants]
FROM dbo.courses INNER JOIN
dbo.employee_courses ON dbo.courses.CourseID = dbo.employee_courses.courseId INNER JOIN
dbo.groups ON dbo.courses.GroupID = dbo.groups.ID RIGHT OUTER JOIN
dbo.employee AS employee_1 INNER JOIN
dbo.Divisions ON employee_1.DivisionCode = dbo.Divisions.SapCode ON dbo.employee_courses.employeeId = employee_1.Username
WHERE (dbo.courses.GroupID = 1)
GROUP BY dbo.courses.CourseID, dbo.courses.CourseName, dbo.Divisions.DivisionShortcut
In addition to be shown above, I want to display the total number of employees in each division and the percent completion in each division, too. By the way, the percent completion is equal to (total number of participants in each course / total number of employees in each division)
So how to do that?
You could use a subquery to add the total employee count to the join on Divisions. For example: