通过连接多个表按类别计算的单个查询

时间:2013-05-04 13:52:56

标签: sql

我有以下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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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]

答案 2 :(得分: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 FIDDLE

正如你可以在小提琴中看到的那样,对于“alice”,例如,课程信息来自不同的行。对于难度级别为1的课程,有一个单独的行,对于其他行也是如此。

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

有人可以告诉我们如何让学生将结果排在一行吗?

答案 3 :(得分: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;

enter image description here