1

我有一张学生表,其中包含一些关于他们是否参加教程等的数据。下面有一个摘录。

|   id |   studentid | name |  present | 
|   1  |   111       | John |       1  | 
|   2  |   222       | Kate |       0  | 
|   3  |   333       | Matt |       1  | 
|   4  |   111       | John |       0  | 
|   5  |   111       | John |       1  | 

我可以计算一下,例如 studentid=111 和 present = 1 以及 studentid=111 和 present = 0 的时间。但我无法解决这两件事。

一世。我可以只做一个计数查询而不是对每个做一个单独的计数吗?目前,我收到了两个单独的查询,例如

studentid=111 并且现在 = 1

| name |  present | 
| John |       12 | 

studentid=111 并且现在 = 0

| name |  absent | 
| John |       4 | 

但理想情况下我会喜欢

| name |  present |  absent | 
| John |       12 |       4 | 

ii.我想看看那些上过不到 75% 课程的学生。我已经阅读了一些关于 avg 函数的内容,但是我不确定如何为此实现一些东西?

4

2 回答 2

2

列出参加过少于 75% 课程的学生:

SELECT
  studentid,
  name, 
  SUM(present) AS present,
  SUM(present = 0) AS absent
FROM your_table
GROUP BY studentid, name
HAVING present < .75 * (present+absent)
于 2012-12-23T10:12:52.930 回答
1

你可以这样做:

SELECT
  studentid,
  name, 
  SUM(present = 1) AS present,
  SUM(present = 0) AS absent
FROM your_table
GROUP BY studentid, name
于 2012-12-23T09:37:49.647 回答