2

我在尝试创建查询时遇到问题:

选择所有在特定周内未完成所有同行评审的学生。

背景:每周,每个学生都必须对同一组的同龄人进行同行评审。

每个组的大小可以不同,这是我遇到的问题。

这是我目前的测试数据:

同行评审表

学生桌

表 1:同行评审表
表 2:学生表。

这是我的初始查询,根据他们所做的同行评审的数量对所有学生进行分组。我现在需要检查是否count(*)小于每个学生的小组规模:

SELECT * 
FROM peerreview
RIGHT JOIN student 
ON student. studentID = peerreview.reviewer
WHERE week = 11
GROUP BY studentID
HAVING Count(*) < ????
4

3 回答 3

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 

见 SqlFiddle

于 2012-05-28T09:55:44.183 回答
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

这会按周为您提供审阅者和他们需要审阅的人员的列表。在真正的解决方案中,您可能希望用不同的周列表替换周的交叉连接,在这些周中应该进行审查。

SQL 小提琴版本

于 2012-05-28T10:18:24.197 回答
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
于 2012-05-28T10:54:41.330 回答