-1

我有以下 3 张桌子。我想写一个查询来计算每个难度级别的学生注册的课程数量,以及注册的课程总数。还应列出未注册的学生。

Students Table:
Student ID      Student Name
1               Alice
2               Bob
3               Charlie
4               David

Courses Table:
Course ID       Course Name             Difficulty Level
1               Arithmetic              1
2               Advanced Calculus       3
3               Algebra                 2
4               Trignometry             2

Enrollment Table:
Enrollment ID   Student ID      Course ID
1               1               1
2               1               3
3               1               4
4               2               2
5               2               3
6               2               4
7               3               3

这是预期的输出:

Output:
Student ID      Student Name    Total Courses       Courses with        Courses with        Courses with
                                Enrolled In         Difficulty Level 1  Difficulty Level 2  Difficulty Level 3
1               Alice           3                   1                   2                   0
2               Bob             3                   0                   2                   1
3               Charlie         1                   0                   1                   0
4               David           0                   0                   0                   0

我很感激这方面的任何帮助。我尝试了几个不同的查询,发现很难找到一个列出所有学生的查询。

4

4 回答 4

1

这将吸引所有学生,无论他们是否注册任何课程

SELECT s.student_id, student_name, count(c.course_id)
, sum(case when difficulty_level = 1 then 1 else 0 end) as level1
, sum(case when difficulty_level = 2 then 1 else 0 end) as level1
, sum(case when difficulty_level = 3 then 1 else 0 end) as level1
FROM students s 
  left outer join enrollment e ON s.student_id = e.student_id
  left outer join courses c ON e.course_id = c.course_id
GROUP BY s.student_id, student_name
于 2013-05-04T13:56:15.797 回答
0
select  s.[Student ID]
,       s.[Student Name]
,       count(c.[Course ID])
,       count(case when c.[Difficulty level] = 1 end)
,       count(case when c.[Difficulty level] = 2 end)
,       count(case when c.[Difficulty level] = 3 end)
from    Students s
left join    
        Enrollment e
on      e.[Student ID] = s.[Student ID]
left join    
        Courses c
on      c.[Course ID] = e.[Course ID]
group by
        s.[Student ID]
,       s.[Student Name]
于 2013-05-04T13:56:15.360 回答
0

嗯,不完全是你想要的,但它与你想要的相似。任何知识渊博的人都可以帮助我进行此查询以在一行中返回结果。从概念上讲,我认为我的查询不能在一行中检索所需的记录。

select s.id,s.name,
        count(c.id) as courses,
        case c.diff
        when 1 then count(c.diff) else 0
        end as "level1",
        case c.diff
        when 2 then count(c.diff) else 0
        end as "level2",
        case c.diff
        when 3 then count(c.diff) else 0
        end as "level3"
from student s
left join enrollment e
on e.st_id=s.id
left join course c
on e.cr_id=c.id
group by s.id,s.name,c.diff

SQL 小提琴

正如您在小提琴中看到的那样,例如,对于“爱丽丝”,课程信息出现在不同的行中。对于 difficutly 级别 1 的课程,有一个单独的行,其他人也是如此。

从概念上讲,当 sql 引擎选择行时,它只会遇到一个课程信息,因此它会连续提供一门课程的信息。

谁能告诉如何让学生的结果排成一行?

于 2013-05-04T14:48:16.430 回答
0
SELECT s.id AS 'Student ID', s.name AS 'Student Name', COUNT(e.c_id) AS 'No of cource', 

COALESCE(SUM( c.lavel = 1 ),0) AS 'Difficult lavel 1',
COALESCE(SUM( c.lavel = 2 ),0) AS 'Difficult lavel 2',
COALESCE(SUM( c.lavel = 3 ),0) AS 'Difficult lavel 3'

FROM students s 
LEFT JOIN enrollment e ON s.id = e.s_id
LEFT JOIN courses c ON c.id = e.c_id
GROUP BY s.id,s.name;

在此处输入图像描述

于 2015-07-22T07:32:08.580 回答