2012-04-30 81 views
3

确定多行,所以我想查询解决:SQL - 在HAVING子句

我想表达以下业务规则:

显示组(特定教程(tutorialID)和周(周)),需要额外的审查。

因此,基本上我需要确定组中的成员数量,然后我需要汇总该周和该组的所有scrum评论,并查看该数量是否等于组中的成员数量。如果是这样,这意味着所有成员都在那一周进行了审核,因此不需要显示。

假设:一个成员每周只能评论一次。

我试过下面的SQL,但是我得到以下错误Subquery returns more than 1 row

SELECT groupName, g.groupId 
FROM `student` s, `group` g 
WHERE s.GroupId = g.GroupId 
AND s.GroupId IS NOT NULL 
AND s.tutorialId = 2 
GROUP by s.GroupId 
AND s.GroupID = (
    SELECT GroupId 
    FROM student 
    GROUP BY GroupId 
    HAVING count(*)> (
     SELECT count(*) 
     FROM scrumreview r, student s 
     WHERE r.reviewee = s.studentID 
     GROUP BY GroupId  
     AND r.week = 5 
     ) 
    ) 

学生 enter image description here

scrumreview enter asdasddescription here

enter image description here

+0

在GROUP BY添加在最里面的子查询 – dgamma3

回答

3

您的内部查询(从第8行开始)返回多个groupid s。相等运算符=比较1对1,但您想要比较的是一对多,您可以使用IN进行比较。

因此,要解决这个问题,在第7行的更改此代码...

AND s.GroupID = (

...这样的:

AND s.GroupID IN (
0
SELECT GroupId 
FROM student 
GROUP BY GroupId 
HAVING count(*)> (
    SELECT count(*) 
    FROM scrumreview r, student s 
    WHERE r.reviewee = s.studentID 
    AND r.week = 5 
    ) 

返回超过1行,或者您需要细化该SQL,以便它只返回一行,或者您需要更改以下行:

AND s.GroupID = (

到IN:

AND s.GroupID IN (
0

试试这个:

SELECT groupName, 
g.groupId FROM `student` s, 
`group` g WHERE s.GroupId = g.GroupId 
AND s.GroupId IS NOT NULL 
AND s.tutorialId = 2 
GROUP by s.GroupId AND s.GroupID in (SELECT GroupId FROM student GROUP BY GroupId 
HAVING count(*)> (SELECT count(*) FROM scrumreview r, student s WHERE r.reviewee = 
s.studentID AND r.week = 5))