我无法尝试创建查询:
选择所有尚未完成特定周所有同行评审的学生。
背景:每周,每个学生都必须同行评审同一组中的同龄人。
每个组可以是不同的大小,这是我遇到的问题。
这是我目前的测试数据:
表1:同行评审表
表2:学生表。
这是我的初步查询,根据他们所做的同行评审数量对所有学生进行分组。我现在需要检查count(*)
是否小于每个学生的小组大小:
SELECT *
FROM peerreview
RIGHT JOIN student
ON student. studentID = peerreview.reviewer
WHERE week = 11
GROUP BY studentID
HAVING Count(*) < ????
答案 0 :(得分:2)
以下查询将返回已审核同一组中所有学生的学生。
SELECT a.reviewer,
a.groupid
FROM (SELECT student2.studentID AS reviewer,
student1.groupid,
Count(*) AS cnt
FROM student student1
INNER JOIN peerreview
ON student1.studentID = peerreview.reviewee
INNER JOIN STUDENT STUDENT2
ON student2.studentID = peerreview.reviewer
WHERE student2.groupid = student2.groupid
AND peerreview.week = 11
GROUP BY student1.groupid,
student2.studentID) a
INNER JOIN (SELECT groupid,
Count(*) - 1 AS cnt
FROM student
GROUP BY groupid) b
ON a.groupid = b.groupid
AND a.cnt = b.cnt
答案 1 :(得分:1)
Select S.StudentId As Reviewer
, S1.StudentId As StudentYetToBeReviewed
, Weeks.WeekNum
From Student As S
Join Student As S1
On S1.GroupId = S.GroupId
And S1.StudentId <> S.StudentId
Cross Join (
Select 7 As WeekNum
Union All Select 11
) As Weeks
Where Not Exists (
Select 1
From PeerReview As P1
Where P1.reviewee = S1.StudentId
And P1.Week = Weeks.WeekNum
)
Order By WeekNum, reviewer
这为您提供了按周查看评论者和他们需要审核的人的列表。在真正的解决方案中,您可能希望将周数的交叉连接替换为应进行审核的周的明确列表。
答案 2 :(得分:0)
select distinct s1.*
from student s1 inner join student s2 on s1.groupId = s2.groupeId
left join peerreview pr on pr.revieweer = s1.studentId
and pr.reviewee = s2.studentId
where pr.Week = ? and pr.revieweer is null and s1.studentId <> s2.studentId