如何显示每个课程中每个部门的完成百分比?

时间:2012-03-30 18:43:30

标签: sql sql-server-2008-r2

我正在开发一个跟踪培训的基于网络的应用程序

为公司的每位员工。我有以下数据库

设计

Employee Table: Username, Name, DivisionCode
Division Table: SapCode, DivisionName
Course Table: CourseID, CourseName, GroupID
Group Table: GroupID, GroupName
Employee_Course Table: Username, CourseID

(每个表中的第一个属性是除最后一个表之外的主键)

我有来实现一个图表或表格,显示这些课程每门课程中每个部门的完成百分比,但我不知道如何提出这个查询。查询应显示DivisionName,CourseName,每个课程中的参与者数量和完成百分比

那么请你帮我解决一下这个问题吗?

1 个答案:

答案 0 :(得分:0)

select a.DivisionName, a.CourseName, a.ParticipantsCount, ParticipantCount / EmployeeCount * 100 as PercentCompleted
from (
    select DivisionName, count(*) as EmployeeCount
    from Division d
    inner join Employee e on d.DivisionCode  = e.DivisionCode 
    group by DivisionName
) dc
inner join (
    select d.DivisionName, c.CourseName, count(*) as ParticipantCount
    from Course c
    inner join Employee_Course ec on c.CourseID = ec.CourseID
    inner join Employee e on ec.Username = e.Username
    inner join Division d on e.DivisionCode  = d.DivisionCode 
    group by DivisionName, CourseName
) a on dc.DivisionName = a.DivisionName