Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - multiple rows in HAVING clause

ok, So the query I would like to solve:

I would like to express the following business rule:

Display the groups (for a particular tutorial (tutorialID) and week (week)) that require additional reviews.

So basically I need to determine the amount of members in a group, I then need to aggregate all the scrum reviews for that week and that group, and see if this amount equals the amount of members in the group. If it does, this means that all members have been reviewed for that week, and therefore does not need to be displayed.

Assumptions: a member can only be reviews once per week.

I've tried the following SQL, however I get the following error 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
        )
    )

Student enter image description here

scrumreview enter asdasddescription here

group enter image description here

like image 929
dgamma3 Avatar asked Mar 06 '26 19:03

dgamma3


1 Answers

Your inner query (starting on line 8) returns multiple groupids. The equality operator = compares 1-to-1, but what you want is to compare 1-to-many, which you can do with IN.

So, to fix this, on line 7 change the code from this...

AND s.GroupID = (

...to this:

AND s.GroupID IN (
like image 190
ean5533 Avatar answered Mar 09 '26 10:03

ean5533