显示COUNT(course.id)的结果为“ 0”

时间:2019-09-10 02:57:20

标签: sql sql-server

我有一个查询,该查询可以正确计算平台中每个课程的学生人数,但是在学生人数= 0的情况下,它不会返回结果。如何修改以下查询以包括此内容?

SQL:

/*  MSSQL
    Name: Courses using the TDC themes with student enrolment count
    Description: Returns a list of courses using a TDC theme and the number of enrolled students in each course.
*/

select
    concat('<a target="_blank" href="%%wwwroot%%/course/index.php?categoryid=',cat.id,'">', cat.name,'</a>') as "Category",
    concat('<a target="_new" href="%%wwwroot%%/course/view.php?id=',course.id,'">',course.fullname,'</a>') as "Course",
    course.theme as "Theme",
    count(course.id) as "Students"
from prefix_course as course
    join prefix_context as context on context.instanceid = course.id
    join prefix_role_assignments as ra on ra.contextid = context.id
    join prefix_course_categories as cat on cat.id = course.category
where context.contextlevel = 50 and ra.roleid = 5 and course.theme like 'tdc%'
group by cat.id, cat.name, course.id, course.fullname, course.theme
order by cat.name

实际结果:

| Category  | Course             | # of Students |
|-----------|--------------------|---------------|
| Baking    | Baking 101         | 7             |
| IT        | Intro to Excel     | 9             |

预期结果:

| Category  | Course             | # of Students |
|-----------|--------------------|---------------|
| Baking    | Baking 101         | 7             |
| IT        | Web Programming    | 0             |
| IT        | Intro to Excel     | 9             |
| Chemistry | Chemical Reactions | 0             |

我已经搜索了一些关于SO的问题(例如here),但无法将答案转化为我的情况。

3 个答案:

答案 0 :(得分:0)

如果将prefix_course表移至左外部联接并使用sum(if(course.id为null,0,1))而不是sum,则它应该起作用:

select
    concat('<a target="_blank" href="%%wwwroot%%/course/index.php?categoryid=',cat.id,'">', cat.name,'</a>') as "Category",
    concat('<a target="_new" href="%%wwwroot%%/course/view.php?id=',course.id,'">',course.fullname,'</a>') as "Course",
    course.theme as "Theme",
    sum(iif(course.id is null,0,1)) as "Students"
from prefix_context as context 
    inner join prefix_role_assignments as ra on ra.contextid = context.id
    inner join prefix_course_categories as cat on cat.id = course.category
    left outer join prefix_course as course on context.instanceid = course.id and course.theme like 'tdc%'
where context.contextlevel = 50 and ra.roleid = 5
group by cat.id, cat.name, course.id, course.fullname, course.theme
order by cat.name

答案 1 :(得分:0)

通过这种分组方式:GROUP BY ALL。这是cte6答案的替代方法。

调整后的查询:

select
    concat('<a target="_blank" href="%%wwwroot%%/course/index.php?categoryid=',cat.id,'">', cat.name,'</a>') as "Category",
    concat('<a target="_new" href="%%wwwroot%%/course/view.php?id=',course.id,'">',course.fullname,'</a>') as "Course",
    course.theme as "Theme",
    count(course.id) as "Students"
from prefix_course as course
    join prefix_context as context on context.instanceid = course.id
    join prefix_role_assignments as ra on ra.contextid = context.id
    join prefix_course_categories as cat on cat.id = course.category
where context.contextlevel = 50 and ra.roleid = 5 and course.theme like 'tdc%'
group by all cat.id, cat.name, course.id, course.fullname, course.theme
order by cat.name

答案 2 :(得分:0)

在解决这个难题之后,我意识到我的问题在于选择执行该任务所需的表。我已经通过以下select语句和表格解决了这个问题。我还使用了不同的伪数据(对不起),所以我用新的伪数据更新了我的OP:

select
    concat('<a target="_blank" href="%%wwwroot%%/course/index.php?categoryid=',cat.id,'">', cat.name,'</a>') as "Category",
    concat('<a target="_new" href="%%wwwroot%%/course/view.php?id=',c.id,'">',c.fullname,'</a>') as "Course",
    c.theme as Theme,
    sum(iif(r.id is null,0,1)) as '# of Students'
from prefix_user as u
    join prefix_role_assignments as ra on u.id = ra.userid
    left outer join prefix_role as r on ra.roleid = r.id and r.id = 5 -- student role id in moodle.
    join prefix_context as ct on ra.contextid = ct.id
    join prefix_course as c on c.id = ct.instanceid and ct.contextlevel = 50 -- 'course' context type.
    join prefix_course_categories as cat on cat.id = c.category and c.theme like 'tdc%'
group by cat.id, c.id, cat.name, c.fullname, c.theme

表格: prefix_course_categories:

|  id | name      |
|-----|-----------|
| 1   | Baking    |
| 2   | Chemistry |
| 3   | IT        |

prefix_course:

|  id | name               | categoryid  | theme        |
|-----|--------------------|-------------|--------------| -- prefix_course.categoryid =
| 8   | Baking 101         | 1           | tdcemuteal   | -- prefix_course_categories.id
| 90  | Web Programming    | 2           | NULL         |
| 287 | Intro to Excel     | 2           | tdcemuaqua   |
| 396 | Chemical Reactions | 3           | tdcemuorange |

prefix_user:

|  id  |
|------|
| 1    |
| 2    |
| 5    |
| 8    |
| 15   |
| 16   |
| 20   |
| 23   |
| 77   |
| 99   |
| 303  |
| 879  |
| 959  |
| 1235 |

prefix_role:

|  id | name                |
|-----|---------------------|
| 1   | Manager             |
| 2   | Course Creator      |
| 3   | Teacher             |
| 4   | Non-Editing Teacher |
| 5   | Student             |

prefix_role_assignments:

|  id |  contextid | userid | roleid |
|-----|------------|--------|--------| -- prefix_role_assignments.contextid = 
| 1   | 1          | 879    | 5      | -- prefix_context.id
| 2   | 2          | 303    | 5      | 
| 3   | 3          | 5      | 5      | -- prefix_role_assignments.userid = 
| 4   | 4          | 15     | 6      | -- prefix_user.id
| 5   | 5          | 57     | 5      |
| 6   | 6          | 23     | 5      | -- prefix_role_assignments.roleid = 
| 7   | 7          | 959    | 5      | -- prefix_role.id
| 8   | 8          | 99     | 5      |
| 9   | 9          | 879    | 5      |
| 10  | 10         | 303    | 5      |
| 11  | 11         | 5      | 5      |
| 12  | 12         | 15     | 5      |
| 13  | 13         | 57     | 5      |
| 14  | 14         | 23     | 5      |
| 15  | 15         | 959    | 5      |
| 16  | 16         | 77     | 5      |
| 17  | 17         | 1235   | 5      |
| 18  | 18         | 8      | 1      |
| 19  | 19         | 23     | 7      |
| 20  | 20         | 287    | 7      |
| 21  | 21         | 287    | 7      |
| 22  | 22         | 287    | 7      |
| 23  | 23         | 287    | 7      |
| 24  | 24         | 699    | 7      |

prefix_context:

|  id |  contextlevel | instanceid |
|-----|---------------|------------|
| 1   | 50            | 8          | -- prefix_context.instanceid =
| 2   | 50            | 8          | -- prefix_course.id 
| 3   | 50            | 8          |
| 4   | 50            | 8          | -- instanceid where contextlevel = 50 are courses that students enrol into.
| 5   | 50            | 8          |
| 6   | 50            | 8          |
| 7   | 50            | 8          |
| 8   | 90            | 8          |
| 9   | 50            | 287        |
| 10  | 50            | 287        |
| 11  | 50            | 287        |
| 12  | 50            | 287        |
| 13  | 50            | 287        |
| 14  | 50            | 287        |
| 15  | 50            | 287        |
| 16  | 50            | 287        |
| 17  | 50            | 287        |
| 18  | 60            | 287        |
| 19  | 50            | 396        |
| 20  | 20            | 287        |
| 21  | 60            | 287        |
| 22  | 10            | 287        |
| 23  | 80            | 287        |
| 24  | 50            | 699        |