我有一张表格,显示学生注册的每门课程。
StudID Course % Symbol GPA Cond1 Cond2
| 1004 | STA2 | 55 | 3 | 24 | Y | Y |
| 1004 | Psy1 | 67 | 2- | 24 | n | Y |
| 1005 | CS3 | 67 | 2- | 36 | Y | Y |
| 1005 | ECO3 | 70 | 2+ | 18 | Y | N |
| 1005 | GAM1 | 77 | 1 | 24 | Y | Y |
| 1005 | GAM2 | 55 | 3 | 36 | Y | Y |
| 1005 | MAM3 | 52 | 3 | 36 | Y | Y |
我如何选择学生做PSY1的课程数量是否同时满足了Cond1和Cond 2的要求?即1004通过两个标准的1个课程,也是psy1
SELECT COUNT(*) FROM tblGrades WHERE Course = 'PSY' GROUP BY StudID;
不起作用。谢谢
ED:我需要一份所有学生参加PSY1的清单,以及他们做的其他课程,其中包括fufill bot cond1和cond2。答案 0 :(得分:0)
未经测试,但应该给你一个启动器:
select t.studid, count(1)
from T
join (
select distinct studid
from T where Course = 'PSY'
) U
on t.studid = u.studid
where t.cond1 = 'Y' and t.cond2 = 'Y'
group by t.studid
答案 1 :(得分:0)
E.g:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(student_id INT NOT NULL
,course_code VARCHAR(12) NOT NULL
,pct INT NOT NULL
,symbol CHAR(3) NOT NULL
,GPA INT NOT NULL
,Condition_1 CHAR(1) NOT NULL
,Condition_2 CHAR(1) NOT NULL
,PRIMARY KEY(student_id,course_code)
);
INSERT INTO my_table VALUES
(1004 ,'STA2', 55, '3' ,24 ,'Y' ,'Y'),
(1004 ,'Psy1', 67, '2-' ,24 ,'n' ,'Y'),
(1005 ,'CS3', 67, '2-' ,36 ,'Y' ,'Y'),
(1005 ,'ECO3', 70, '2+' ,18 ,'Y' ,'N'),
(1005 ,'GAM1', 77, '1' ,24 ,'Y' ,'Y'),
(1005 ,'GAM2', 55, '3' ,36 ,'Y' ,'Y'),
(1005 ,'MAM3', 52, '3' ,36 ,'Y' ,'Y');
SELECT x.student_id
, COUNT(*) other_courses_total
FROM my_table x
JOIN my_table y
ON y.course_code <> x.course_code
AND y.student_id = x.student_id
WHERE y.course_code = 'Psy1'
AND x.condition_1 = 'Y'
AND x.condition_2 = 'Y'
GROUP
BY x.student_id;
+------------+---------------------+
| student_id | other_courses_total |
+------------+---------------------+
| 1004 | 1 |
+------------+---------------------+
如果您想要将分数为零的学生包括在内,请将[INNER] JOIN更改为LEFT JOIN和COUNT(或COALESCE(COUNT))。