无法弄清楚为什么SQL返回的行多于预期

时间:2015-12-16 16:36:52

标签: sql sql-server

我正在根据某些SQL的结果构建报告。有一个赛事有3名评委得分。我在夜间遇到特定战斗的问题(事件是一场战斗)。这是我的代码:

USE DatabaseName;
DECLARE @EventID INT = ;
DECLARE @FightID INT = ;
----Judge 1
SELECT DISTINCT
        JudgeNames.FirstName + ' ' + JudgeNames.LastName AS [Judge Name] ,
        JudgeNames.PersonID ,
        Event.EventID ,
        Fights.FightID ,
        FightScores.RoundNumber ,
        FightScores.Contestant_1_PointsByRound ,
        FightScores.Contestant_1_PointsDeducted ,
        FightScores.Contestant_2_PointsByRound ,
        FightScores.Contestant_2_PointsDeducted
INTO    #Judge1
FROM    dbo.tblEvents Event
        INNER JOIN dbo.tblFights Fights ON Event.EventID = Fights.EventID
        INNER JOIN dbo.tblFightJudge FightJudge ON FightJudge.fightid = Fights.FightID
        INNER JOIN dbo.tblPersons JudgeNames ON JudgeNames.PersonID = FightJudge.judge1id
        INNER JOIN dbo.tblEventJudge EJ ON EJ.EventID = Event.EventID
                                           AND EJ.Judge_PersonID = JudgeNames.PersonID
        INNER JOIN dbo.tblFightRoundScore FightScores ON Fights.FightID = FightScores.FightID
                                                         AND FightScores.EventJudgeID = EJ.EventJudgeID
WHERE   Event.EventID = @EventID
        AND Fights.FightID = @FightID;
----Judge 2
SELECT DISTINCT
        JudgeNames.FirstName + ' ' + JudgeNames.LastName AS [Judge Name] ,
        JudgeNames.PersonID ,
        Event.EventID ,
        Fights.FightID ,
        FightScores.RoundNumber ,
        FightScores.Contestant_1_PointsByRound ,
        FightScores.Contestant_1_PointsDeducted ,
        FightScores.Contestant_2_PointsByRound ,
        FightScores.Contestant_2_PointsDeducted
INTO    #Judge2
FROM    dbo.tblEvents Event
        INNER JOIN dbo.tblFights Fights ON Event.EventID = Fights.EventID
        INNER JOIN dbo.tblFightJudge FightJudge ON FightJudge.fightid = Fights.FightID
        INNER JOIN dbo.tblPersons JudgeNames ON JudgeNames.PersonID = FightJudge.judge2id
        INNER JOIN dbo.tblEventJudge EJ ON EJ.EventID = Event.EventID
                                           AND EJ.Judge_PersonID = JudgeNames.PersonID
        INNER JOIN dbo.tblFightRoundScore FightScores ON Fights.FightID = FightScores.FightID
                                                         AND FightScores.EventJudgeID = EJ.EventJudgeID
WHERE   Event.EventID = @EventID
        AND Fights.FightID = @FightID;
----Judge 3
SELECT DISTINCT
        JudgeNames.FirstName + ' ' + JudgeNames.LastName AS [Judge Name] ,
        JudgeNames.PersonID ,
        Event.EventID ,
        Fights.FightID ,
        FightScores.RoundNumber ,
        FightScores.Contestant_1_PointsByRound ,
        FightScores.Contestant_1_PointsDeducted ,
        FightScores.Contestant_2_PointsByRound ,
        FightScores.Contestant_2_PointsDeducted
INTO    #Judge3
FROM    dbo.tblEvents Event
        INNER JOIN dbo.tblFights Fights ON Event.EventID = Fights.EventID
        INNER JOIN dbo.tblFightJudge FightJudge ON FightJudge.fightid = Fights.FightID
        INNER JOIN dbo.tblPersons JudgeNames ON JudgeNames.PersonID = FightJudge.judge3id
        INNER JOIN dbo.tblEventJudge EJ ON EJ.EventID = Event.EventID
                                           AND EJ.Judge_PersonID = JudgeNames.PersonID
        INNER JOIN dbo.tblFightRoundScore FightScores ON Fights.FightID = FightScores.FightID
                                                         AND FightScores.EventJudgeID = EJ.EventJudgeID
WHERE   Event.EventID = @EventID
        AND Fights.FightID = @FightID;
----Fight Info
SELECT DISTINCT
        Ref.FirstName + ' ' + Ref.LastName AS [Ref Name] ,
        Fights.EventID ,
        Fights.FightID ,
        Fights.Rounds ,
        Fights.ContestantID_1 ,
        Fights.ContestantID_2 ,
        C1.FirstName + ' ' + C1.LastName AS Fighter1 ,
        C2.FirstName + ' ' + C2.LastName AS Fighter2 ,
        Fights.Contestant1CornerColor AS Contestant1CornerColorHEX ,
        Fights.Contestant2CornerColor AS Contestant2CornerColorHEX ,
        Events.EventDate ,
        Fights.Fight_WeightClass ,
        Fights.FightNumber ,
        ( SELECT    COUNT(FightNumber)
          FROM      dbo.tblFights
          WHERE     EventID = Fights.EventID
        ) AS NumOfFights
INTO    #FightInfo
FROM    dbo.tblFights Fights
        INNER JOIN dbo.tblPersons Ref ON Fights.Referee_PersonID = Ref.PersonID
        INNER JOIN dbo.tblEvents Events ON Fights.EventID = Events.EventID
        INNER JOIN dbo.tblPersons C1 ON C1.PersonID = Fights.ContestantID_1
        INNER JOIN dbo.tblPersons C2 ON C2.PersonID = Fights.ContestantID_2
WHERE   Fights.EventID = @EventID
        AND Fights.FightID = @FightID;
--MainQuery
SELECT  DISTINCT
        FI.EventID ,
        FI.FightID ,
        FI.FightNumber ,
        FI.NumOfFights ,
        FI.Rounds ,
        FI.EventDate ,
        FI.[Ref Name] ,
        FI.Fight_WeightClass ,
        FI.Contestant1CornerColorHEX ,
        FI.Contestant2CornerColorHEX ,
        FI.Fighter1 ,
        FI.Fighter2 ,
        #Judge1.[Judge Name] AS Judge1 ,
        #Judge2.[Judge Name] AS Judge2 ,
        #Judge3.[Judge Name] AS Judge3 ,
        ISNULL(#Judge1.RoundNumber, 1) AS RoundNumber ,
        --Judge 1
        #Judge1.Contestant_1_PointsByRound AS J1C1Points ,
        #Judge1.Contestant_1_PointsDeducted AS J1C1Deduct ,
        ( #Judge1.Contestant_1_PointsByRound
          - #Judge1.Contestant_1_PointsDeducted ) AS J1C1Total ,
        #Judge1.Contestant_2_PointsByRound AS J1C2Points ,
        #Judge1.Contestant_2_PointsDeducted AS J1C2Deduct ,
        ( #Judge1.Contestant_2_PointsByRound
          - #Judge1.Contestant_2_PointsDeducted ) AS J1C2Total ,
        --Judge 2
        #Judge2.Contestant_1_PointsByRound AS J2C1Points ,
        #Judge2.Contestant_1_PointsDeducted AS J2C1Deduct ,
        ( #Judge2.Contestant_1_PointsByRound
          - #Judge2.Contestant_1_PointsDeducted ) AS J2C1Total ,
        #Judge2.Contestant_2_PointsByRound AS J2C2Points ,
        #Judge2.Contestant_2_PointsDeducted AS J2C2Deduct ,
        ( #Judge2.Contestant_2_PointsByRound
          - #Judge2.Contestant_2_PointsDeducted ) AS J2C2Total ,
          --Judge3
        #Judge3.Contestant_1_PointsByRound AS J3C1Points ,
        #Judge3.Contestant_1_PointsDeducted AS J3C1Deduct ,
        ( #Judge3.Contestant_1_PointsByRound
          - #Judge3.Contestant_1_PointsDeducted ) AS J3C1Total ,
        #Judge3.Contestant_2_PointsByRound AS J3C2Points ,
        #Judge3.Contestant_2_PointsDeducted AS J3C2Deduct ,
        ( #Judge3.Contestant_2_PointsByRound
          - #Judge3.Contestant_2_PointsDeducted ) AS J3C2Total
INTO    #MQ1
FROM    #FightInfo FI
        INNER JOIN #Judge1 ON #Judge1.FightID = FI.FightID
        INNER JOIN #Judge2 ON #Judge2.FightID = FI.FightID
        INNER JOIN #Judge3 ON #Judge3.FightID = FI.FightID
WHERE   FI.FightID = @FightID
        AND FI.EventID = @EventID
ORDER BY FI.FightNumber;

SELECT  *
FROM    #Judge1;
SELECT  *
FROM    #Judge2;
SELECT  *
FROM    #Judge3;
SELECT  *
FROM    #FightInfo;
SELECT  *
FROM    #MQ1;

DROP TABLE #FightInfo,#Judge1,#Judge2,#Judge3,#MQ1;

3位评委中的每位评委返回3轮评分如下: enter image description here

“FightInfo”查询按预期返回单行,如: enter image description here

当我运行最终查询时出现问题。法官3每轮返回2组积分。这导致我有6行数据,如下所示: enter image description here

我为最后一张图片太小而道歉,但正如你所看到的那样,返回了6行。它是由具有不同“PointsByRound”数字的法官3引起的,尽管我似乎无法看到原因或原因在哪里引起。有人可以提供一些帮助来帮助我看看吗?

4 个答案:

答案 0 :(得分:1)

在#MQ1表的连接上添加RoundNumber。

如果您获得27行,如果您加入fightid,则会出现此问题。因为fightids匹配所有行。

    INNER JOIN #Judge1 ON #Judge1.FightID = FI.FightID 
    INNER JOIN #Judge2 ON #Judge2.FightID = FI.FightID AND #Judge2.RoundNumber = #Judge1.RoundNumber
    INNER JOIN #Judge3 ON #Judge3.FightID = FI.FightID AND #Judge3.RoundNumber = #Judge1.RoundNumber

答案 1 :(得分:0)

排除您在distinct子句中返回的一些列以进行测试。

通过这种方式,您可能会发现问题出在使用计算的列中。

答案 2 :(得分:0)

快速解决方法是使用不同的

SELECT DISTINCT * FROM#MQ1;

答案 3 :(得分:0)

我在屏幕上看不到明显的问题,所以这就是我要做的调试。

  • 别名所有表名。庞大的查询更难阅读。
  • 将所有三名裁判放入一个带有参数的裁判信息表中,以指定判断ID /数字/为此目的所需的任何内容。您将获得更少的重复逻辑,这将使这更容易阅读和维护,并且可以使用连接条件中的不同参数连接到同一个表3次,以获得最终结果。
  • 一旦三名评委在一张桌子上,我怀疑你的所有得分计算都可以在CTE /临时表中完成。再次,重复逻辑较少。

到现在为止,我希望清理工作能够找到并修复问题:-)如果它仍然存在 -

  • 在最终查询中取消DISTINCT。你不应该从我能看到的那个查询中获得它,它可以掩盖连接问题。
  • 评论与评委1和评委2相关的所有内容,只是为了确保它们确实他们正在以某种方式创建的问题。
  • 在你获得绝对最低结果之前,几乎要注意一切。您可以运行的最基本的查询是什么,为您提供6个问题行而不是您想要的3个?这应该表明问题所在。