我正在根据某些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每轮返回2组积分。这导致我有6行数据,如下所示:
我为最后一张图片太小而道歉,但正如你所看到的那样,返回了6行。它是由具有不同“PointsByRound”数字的法官3引起的,尽管我似乎无法看到原因或原因在哪里引起。有人可以提供一些帮助来帮助我看看吗?
答案 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)
我在屏幕上看不到明显的问题,所以这就是我要做的调试。
到现在为止,我希望清理工作能够找到并修复问题:-)如果它仍然存在 -