2

我需要找到与他们关联的客户数量最少的人的 ID,以便将当前的新客户分配给该员工。

我使用以下内容来建立该员工的ID,如下所示;

    SELECT TOP(1) JA.judgeID, COUNT(JA.judgeId) AS COUNT
    FROM recJudgeAssignment AS JA
    LEFT JOIN recEntrantStatus AS ES
    ON JA.bandId = ES.entrantId
    GROUP BY JA.judgeId, ES.roundId
    HAVING ES.roundId = @round
    ORDER BY COUNT

但我需要将其作为局部变量捕获,以便在下一条语句中使用;

    UPDATE recJudgeAssignment
    SET judgeId = @judge
    WHERE roundId = @round
    AND bandId = @entrantId

我如何在第一个选择中捕捉到 JudgeId?通常我会做类似的事情;

    SELECT @judge =(SELECT TOP(1) JA.judgeID, COUNT(JA.judgeId) AS COUNT
    FROM recJudgeAssignment AS JA
    LEFT JOIN recEntrantStatus AS ES
    ON JA.bandId = ES.entrantId
    GROUP BY JA.judgeId, ES.roundId
    HAVING ES.roundId = @round
    ORDER BY COUNT)

这当然是行不通的,因为 select 会返回 JudgeId 和 COUNT。我宁愿不必将第一次选择的结果发送回网页,然后在单独的 web>db 事务中执行后续更新语句。

所以......任何人都可以提供的任何帮助甚至补救措施,将不胜感激。

提前致谢!

4

3 回答 3

3
-- Query the DB for your variables
-- (I have also moved the HAVING clause to a WHERE clause.)
------------------------------------------------------------
SELECT TOP(1)
  @judgeID = JA.judgeID,
  @count   = COUNT(JA.judgeId)
FROM
  recJudgeAssignment AS JA
LEFT JOIN
  recEntrantStatus AS ES
    ON JA.bandId = ES.entrantId
WHERE
  ES.roundId = @round
GROUP BY
  JA.judgeId, ES.roundId
ORDER BY
  COUNT

-- Update the DB
------------------------------------------------------------
UPDATE
  recJudgeAssignment
SET
  judgeId = @judgeID
WHERE
      roundId = @round
  AND bandId  = @entrantId

-- Return the variables to the client
------------------------------------------------------------
SELECT
  @judgeID     AS judgeID,
  @count       AS count
于 2012-09-24T13:53:00.593 回答
2
SELECT top(1) @judge = JA.judgeID, @count_j =COUNT(JA.judgeId)
    FROM recJudgeAssignment AS JA
    LEFT JOIN recEntrantStatus AS ES
    ON JA.bandId = ES.entrantId
    GROUP BY JA.judgeId, ES.roundId
    HAVING ES.roundId = @round
    ORDER BY COUNT
于 2012-09-24T13:51:13.283 回答
1

只看第一个查询
请试试这个

SELECT TOP(1) JA.judgeID
FROM recJudgeAssignment AS JA
LEFT JOIN recEntrantStatus AS ES
 ON   JA.bandId = ES.entrantId 
 AND  ES.roundId = @round   
GROUP BY JA.judgeId, ES.roundId
ORDER BY COUNT(JA.judgeId)
于 2012-09-24T14:07:29.037 回答