C# 如何显示公司各部门的培训统计?

C# 如何显示公司各部门的培训统计?,c#,asp.net,sql,sql-server,asp.net-charts,C#,Asp.net,Sql,Sql Server,Asp.net Charts,我是一名新的ASP.NET开发人员,现在我正在开发一个web应用程序,作为公司的培训管理系统。我现在正在进行最后一项任务,这项任务是开发一个仪表板,其中显示两个图表,显示以下内容: Courses Table consists of: CourseName, CourseID, GroupID Groups Table consists of: ID, GroupName Employee Table consists of: Name, SSN, Department Employee_Cou

我是一名新的ASP.NET开发人员,现在我正在开发一个web应用程序,作为公司的培训管理系统。我现在正在进行最后一项任务,这项任务是开发一个仪表板,其中显示两个图表,显示以下内容:

Courses Table consists of: CourseName, CourseID, GroupID
Groups Table consists of: ID, GroupName
Employee Table consists of: Name, SSN, Department
Employee_Course Table consists of: employeeId, courseId
  • 图表显示了每个部门有多少员工参加了三种必修课程的统计数据

  • 另一张图表显示了公司每月每周培训的整体百分比

  • 我知道如何使用ASP.NET图表控件,并且我已经开发了两个不同的图表,这两个图表是剩余的

    我现在的问题是SQLServer查询,我需要获得这两个图表的结果

    我的数据库设计如下:

    Courses Table consists of: CourseName, CourseID, GroupID
    Groups Table consists of: ID, GroupName
    Employee Table consists of: Name, SSN, Department
    Employee_Course Table consists of: employeeId, courseId
    
    我提出了以下复杂的查询,但它需要更多的补充

    SELECT TOP (100) PERCENT dbo.employee.Department, dbo.employee.Name, T1.SSN
       , courses_2.CourseName
       , CASE
            WHEN dbo.employee_courses.courseId IS NULL THEN ' '
            ELSE 'Yes'
         END AS CourseId
    FROM dbo.employee_courses
    RIGHT OUTER JOIN dbo.courses AS courses_2
    INNER JOIN
    (
       SELECT employee_1.SSN, courses_1.CourseID
       FROM dbo.employee AS employee_1
       CROSS JOIN dbo.courses AS courses_1
    ) AS T1 ON courses_2.CourseID = T1.CourseID
    INNER JOIN dbo.employee ON T1.SSN = dbo.employee.SSN
       ON dbo.employee_courses.employeeId = T1.SSN
       AND dbo.employee_courses.courseId = T1.CourseID
    ORDER BY T1.SSN
    
    我不知道如何使其适用于显示上述要求

    为了澄清这个问题,让我们假设我们有两种类型/组的课程;强制性和选择性。我们也有部门,;A、 B和C.假设每个部门完成必修课程的员工人数如下: A部门:105名员工中有55名, B部门:114名员工中有78人, C部门:147名员工中有98人


    我想要这样一个查询,让我知道每个部门完成必修课和选修课的员工百分比。因此,我可以开发一个图表,显示每个部门的两列(或两个系列),显示每个部门的培训百分比。

    听起来你需要做一些汇总

    类似这样的内容应该可以让您按部门统计参加特定课程的员工人数

    SELECT COUNT(*), CourseID, Department
    FROM Courses c
    INNER JOIN Employee_Course ec ON c.CourseID = ec.CourseID
    INNER JOIN Employee e ON ec.EmployeeID = e.EmployeeID
    HAVING CourseID IN (requiredCourseIDs)
    GROUP BY CourseID, GroupID
    

    这是否在正确的轨道上?

    下面的查询返回我所理解的您正在查找的信息。key子句在T-SQL中使用EXCEPT语句。Exception将返回第一个查询中未包含在第二个查询中的所有行。我们可以在这里使用它来确定尚未完成所有必修课程或所有选修课程的员工。为了清晰起见,我使用了常用的表表达式,但您也可以将CTE用作子查询

    ;with EmployeesWithRequiredCourses as
    (
        select *
        from Employee
        where not exists
        (
            select CourseID
            from Courses
            inner join GroupTable on GroupTable.GroupID = Courses.GroupID
            where GroupTable.GroupName = 'Required'
    
            except
    
            select Employee_Course.CourseID
            from Employee_Course
            inner join Courses on Courses.CourseID = Employee_Course.CourseID
            inner join GroupTable on GroupTable.GroupID = Courses.GroupID
            where Employee_Course.EmployeeID = Employee.EmployeeID
            and GroupTable.GroupName = 'Required'
        )
    ),
    EmployeesWithOptionalCourses as
    (
        select *
        from Employee
        where not exists
        (
            select CourseID
            from Courses
            inner join GroupTable on GroupTable.GroupID = Courses.GroupID
            where GroupTable.GroupName = 'Optional'
    
            except
    
            select Employee_Course.CourseID
            from Employee_Course
            inner join Courses on Courses.CourseID = Employee_Course.CourseID
            inner join GroupTable on GroupTable.GroupID = Courses.GroupID
            where Employee_Course.EmployeeID = Employee.EmployeeID
            and GroupTable.GroupName = 'Optional'
        )
    )
    select  Employee.Department, 
            COUNT(EmployeesWithRequiredCourses.EmployeeID) as RequiredCourseCount,
            COUNT(EmployeesWithOptionalCourses.EmployeeID) as OptionalCourseCount,
            COUNT(Employee.EmployeeID) as EmployeeCount,
            CAST(COUNT(EmployeesWithRequiredCourses.EmployeeID) as real)/CAST(COUNT(Employee.EmployeeID) as real) as RequiredCoursePercentage,
            CAST(COUNT(EmployeesWithOptionalCourses.EmployeeID) as real)/CAST(COUNT(Employee.EmployeeID) as real) as OptionalCoursePercentage
    from Employee
    left outer join EmployeesWithRequiredCourses on EmployeesWithRequiredCourses.EmployeeID = Employee.EmployeeID
    left outer join EmployeesWithOptionalCourses on EmployeesWithOptionalCourses.EmployeeID = Employee.EmployeeID
    group by Employee.Department
    
    但是,根据您在下面的评论,我编写了一个新的查询,列出了每个部门和课程名称、完成该课程的员工人数、该部门的员工总数以及该部门完成该课程的员工百分比。和前面一样,我使用公共表表达式,但您可以轻松地将其转换为使用子查询

    ;with Departments as
    (
        select Department, COUNT(*) as DepartmentEmployeeCount
        from Employee
        group by Department
    ),
    DepartmentCourse as
    (
        select Department, CourseName, DepartmentEmployeeCount
        from Departments
        cross join Courses
    ),
    CompletedCourses as
    (
        select Department, CourseName, COUNT(*) as CourseCompletedCount
        from Employee
        inner join Employee_Course on Employee_Course.EmployeeID = Employee.EmployeeID
        inner join Courses on Courses.CourseID = Employee_Course.CourseID
        group by Department, CourseName
    )
    select  DepartmentCourse.Department, 
            DepartmentCourse.CourseName, 
            CourseCompletedCount, 
            DepartmentEmployeeCount,
            CAST(ISNULL(CourseCompletedCount,0) as real)/CAST(DepartmentEmployeeCount as real) as CourseCompletionPercentage
    from DepartmentCourse
    left outer join CompletedCourses on CompletedCourses.Department = DepartmentCourse.Department and CompletedCourses.CourseName = DepartmentCourse.CourseName
    
    我还包括了我用来设置一些测试数据的SQL,这样您就可以看到我所做的与您自己的数据库不匹配的假设

    create table GroupTable
    (
        GroupID int not null,
        GroupName varchar(50)
    )
    
    
    create table Courses
    (
        CourseID int not null,
        GroupID int,
        CourseName varchar(50)
    )
    
    create table Employee
    (
        EmployeeID int not null,
        Name varchar(50),
        SSN varchar(11),
        Department varchar(50)
    )
    
    create table Employee_Course
    (
        EmployeeID int not null,
        CourseID int not null
    )
    
    insert into GroupTable values (1, 'Required')
    insert into GroupTable values (2, 'Optional')
    
    insert into Courses values (1, 1, 'Course1')
    insert into Courses values (2, 1, 'Course2')
    insert into Courses values (3, 1, 'Course3')
    insert into Courses values (4, 2, 'Course4')
    insert into Courses values (5, 2, 'Course5')
    insert into Courses values (6, 2, 'Course6')
    
    insert into Employee values (1, 'Bob','122-45-1111', 'A')
    insert into Employee values (2, 'Peter','124-45-2222', 'A')
    insert into Employee values (3, 'Joe','125-45-3333', 'A')
    insert into Employee values (4, 'Jimmy','126-45-4444', 'A')
    insert into Employee values (5, 'Mary','127-45-5555', 'A')
    insert into Employee values (6, 'Alice','122-45-6666', 'B')
    insert into Employee values (7, 'Jennifer','124-45-7777', 'B')
    insert into Employee values (8, 'Carter','125-45-8888', 'B')
    insert into Employee values (9, 'Mason','126-45-9999', 'C')
    insert into Employee values (10, 'Irina','127-45-0000', 'C')
    
    insert into Employee_Course values (1,1)
    insert into Employee_Course values (1,2)
    insert into Employee_Course values (1,3)
    insert into Employee_Course values (1,4)
    insert into Employee_Course values (1,5)
    insert into Employee_Course values (1,6)
    insert into Employee_Course values (2,1)
    insert into Employee_Course values (2,2)
    insert into Employee_Course values (2,4)
    insert into Employee_Course values (2,5)
    insert into Employee_Course values (3,1)
    insert into Employee_Course values (3,4)
    insert into Employee_Course values (4,1)
    insert into Employee_Course values (4,2)
    insert into Employee_Course values (4,3)
    insert into Employee_Course values (5,4)
    insert into Employee_Course values (5,5)
    insert into Employee_Course values (5,6)
    insert into Employee_Course values (6,1)
    insert into Employee_Course values (6,2)
    insert into Employee_Course values (6,3)
    insert into Employee_Course values (6,4)
    insert into Employee_Course values (6,5)
    insert into Employee_Course values (7,4)
    insert into Employee_Course values (8,1)
    insert into Employee_Course values (9,2)
    insert into Employee_Course values (9,3)
    insert into Employee_Course values (9,4)
    insert into Employee_Course values (9,5)
    insert into Employee_Course values (9,6)
    insert into Employee_Course values (10,1)
    insert into Employee_Course values (10,2)
    insert into Employee_Course values (10,3)
    insert into Employee_Course values (10,4)
    insert into Employee_Course values (10,5)
    insert into Employee_Course values (10,6)
    

    分组表是为各系准备的吗?不,是为课程准备的,因为我有三种必修课。你的问题不清楚。您能指定在结果查询中要查找哪些列以及它们应该包含哪些信息吗?我让这个问题比以前更明显了。希望我能得到你们的帮助。非常感谢你们,伯特。我真的很感谢你的帮助。你帮了我很多。很抱歉这么晚才回复,但我花了很多时间来理解您组织有序的复杂查询。另外,我花了很多时间使其适用于我的数据库,因为我在其中添加了一些更改。顺便说一下,您的查询显示了每个系完成必修课程的百分比。我想显示的是每个课程名称的百分比,而不是课程组。我试图修改您的查询,但没有得到任何结果。我在答案中添加了一个新的查询,其中列出了按部门和课程列出的完成情况。不知什么原因,我以为你希望把所有必修课都分组。再次感谢你。伯特,你的查询是惊人和强大的。它适用于您的表,但当我将查询用于我的表时,它不起作用。我不知道为什么。我只是在上面提到的基础上稍微修改了一下我的数据库。此外,我调整了您的查询以适用于我的新数据库设计,但它曾经工作过一次,然后,它确实工作了。如果不知道您的模式或错误,很难知道问题是什么。您可以通过我个人资料中的电子邮件与我联系,详细介绍您的模式。