SQL获得5个最不忙的工作人员

时间:2015-12-14 15:26:46

标签: sql tsql stored-procedures

我正在开展一项在线竞赛,允许评委对参赛者进行投票。当前一轮比赛结束时,它已经关闭,并且已经做了很多事情来建立下一轮比赛。

目前的系统格式,即所有参与下一轮投票的人都将由所有竞争评委评判。我在存储过程中使用以下代码执行此操作:

INSERT INTO recEntrantStatus (entrantId, roundId, judgeId, notified, voted, enterNextRound)
    SELECT r.entrantId, (@input + 1), j.judgeId /*Now getting tblJudges Id*/, 0, 0, 0
    FROM recEntrantStatus r
        -- Get all of the judges
        CROSS JOIN (SELECT DISTINCT Id AS judgeId FROM recMembers
                     WHERE Privilege >= 2) AS j
        WHERE r.roundId = @input
          AND r.voted = 1
          AND r.enterNextround = 1

您将看到这从recEntrantStatus获取参赛者详细信息,以及来自recMembers的评委数据。所以基本上找到每个投票进入第2轮的人,然后为每个参赛者创造一个记录。所以在第二轮中,每位法官都会评判每一位参赛者。

然而,比赛已经改变,我需要更新存储程序,将每位参赛者分配给五名评委,而不是所有评委。此外,所有参赛者应均匀分配给每位裁判。

之前我使用过以下代码:

SELECT TOP(5) MEM.Id, ISNULL(ES.EntrantCount, 0)
FROM recMembers MEM
    LEFT JOIN
    (
        SELECT judgeid, COUNT(judgeid) 'EntrantCount'
        FROM recEntrantStatus
        WHERE roundId = 1
        GROUP BY judgeid
        ) ES ON MEM.Id = ES.judgeid
WHERE MEM.Privilege = 2
ORDER BY EntrantCount   

这将检查recEntrantStatus中的所有记录,由judgeId'对它们进行分组,然后对它们进行计数。分配给他们的参赛者/记录数最少的5位评委将被退回。

但是,我尝试使用两者的组合 - 以下 - 但是无法运行或保存:

INSERT INTO recEntrantStatus (entrantId, roundId, judgeId, notified, voted, enterNextRound)
    SELECT r.entrantId, (@input + 1), j.judgeId /*Now getting tblJudges Id*/, 0, 0, 0
    FROM recEntrantStatus r
    -- Get all of the judges
        CROSS JOIN (SELECT TOP(5) MEM.Id, ISNULL(ES.EntrantCount, 0)
                    FROM recMembers MEM
                    LEFT JOIN
                    (
                        SELECT judgeid, COUNT(judgeid) 'EntrantCount'
                        FROM recEntrantStatus
                        WHERE roundId = 1
                        GROUP BY judgeid
                    ) ES ON MEM.Id = ES.judgeid
                    WHERE MEM.Privilege = 2
                    ORDER BY EntrantCount ) AS j
    WHERE r.roundId = @input
      AND r.voted = 1
      AND r.enterNextround = 1

我得到的错误是:

  

没有为' j'。

的第2列指定列名

  

列名称无效' judgeId'。

非常感谢任何有关解决此问题的帮助。

1 个答案:

答案 0 :(得分:1)

您有两个问题:正如Adam V在评论中指出的那样,您没有为j查询中的两个字段中的一个命名。此外,您的字段名称judgeID错误 - 您需要提取的内容仅为Id

INSERT INTO recEntrantStatus (entrantId, roundId, judgeId, notified, voted, enterNextRound)
    SELECT 
        r.entrantId, 
        (@input + 1), 
        j.Id /*Now getting tblJudges Id*/,  -- fixed name
        0, 
        0, 
        0
    FROM recEntrantStatus r
    -- Get all of the judges
        CROSS JOIN (SELECT TOP(5) MEM.Id, ISNULL(ES.EntrantCount, 0) EntrantCount
                    FROM recMembers MEM
                    LEFT JOIN
                    (
                        SELECT judgeid, COUNT(judgeid) AS 'EntrantCount' -- name this
                        FROM recEntrantStatus
                        WHERE roundId = 1
                        GROUP BY judgeid
                    ) ES ON MEM.Id = ES.judgeid
                    WHERE MEM.Privilege = 2
                    ORDER BY EntrantCount ) AS j
    WHERE r.roundId = @input
      AND r.voted = 1
      AND r.enterNextround = 1