当我想更新fixtures表时,尝试完成查找表并继续接收空值。我假设因为我试图将逻辑从硬代码更改为查找表,所以它无效。
下面显示了四个表格(从上到下): - 得分了 - ScoreReference - 夹具 - TeamWeighting
我想尝试确定每个夹具的双方之间的团队重量的差异,并根据加权,它通过选择与目标差异相匹配的分数来确定更新“夹具”表的分数。 'TeamWeighting'。
如您所见,它显示为NULL,我不知道为什么会这样。
以下是我知道导致问题的查询代码:
;WITH cte AS
(
SELECT f.FixtureID,
ht.FinalTeamWeight - at.FinalTeamWeight AS TeamScore
FROM dbo.Fixture f
OUTER APPLY
(
SELECT tw.FinalTeamWeight
FROM dbo.TeamWeighting tw
WHERE tw.TeamID = f.HomeTeamID
) ht --HomeTeam
OUTER APPLY
(
SELECT tw.FinalTeamWeight
FROM dbo.TeamWeighting tw
WHERE tw.TeamID = f.AwayTeamID
) at --AwayTeam
WHERE f.HomeScore IS NULL AND f.FixtureDate < GETDATE()
)
UPDATE f
SET f.HomeScore = s.HomeScore,
f.AwayScore = s.AwayScore
FROM dbo.Fixture f
INNER JOIN
(
SELECT FixtureID,
TeamScore,
(
SELECT TOP (1)
CASE
WHEN c.TeamScore BETWEEN HomeWeighting AND AwayWeighting
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore ORDER BY NEWID())
WHEN c.TeamScore BETWEEN AwayWeighting AND HomeWeighting
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE AwayScore > HomeScore ORDER BY NEWID())
END AS ScoreID
FROM dbo.Score s
LEFT JOIN ScoreReference sr
ON s.ScoreDifference = sr.ScoreDifference
) AS ScoreID -- end select case
FROM cte c
) -- end inner join
AS ScoreResult
ON f.FixtureID = ScoreResult.FixtureID
INNER JOIN Score s
ON ScoreResult.ScoreID = s.ScoreID
INNER JOIN ScoreReference sr
ON s.ScoreDifference = sr.ScoreDifference
Select * from dbo.Score
Select * from dbo.ScoreReference
SELECT * from dbo.Fixture
SELECT * From TeamWeighting
END
答案 0 :(得分:1)
试试这个。
;WITH cte AS
(
SELECT f.FixtureID,
ht.FinalTeamWeight - at.FinalTeamWeight AS TeamScore
FROM dbo.Fixture f
OUTER APPLY
(
SELECT tw.FinalTeamWeight
FROM dbo.TeamWeighting tw
WHERE tw.TeamID = f.HomeTeamID
) ht --HomeTeam
OUTER APPLY
(
SELECT tw.FinalTeamWeight
FROM dbo.TeamWeighting tw
WHERE tw.TeamID = f.AwayTeamID
) at --AwayTeam
WHERE f.HomeScore IS NULL AND f.FixtureDate < GETDATE()
)
UPDATE f
SET f.HomeScore = s.HomeScore,
f.AwayScore = s.AwayScore
FROM dbo.Fixture f
INNER JOIN
(
SELECT FixtureID,
TeamScore,
(
SELECT TOP (1)
CASE
WHEN c.TeamScore BETWEEN HomeWeighting AND AwayWeighting
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore ORDER BY NEWID())
WHEN c.TeamScore BETWEEN AwayWeighting AND HomeWeighting
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE AwayScore > HomeScore ORDER BY NEWID())
END AS ScoreID
FROM dbo.Score s
INNER JOIN ScoreReference sr
ON s.ScoreDifference = sr.ScoreDifference
) AS ScoreID -- end select case
FROM cte c
) -- end inner join
AS ScoreResult
ON f.FixtureID = ScoreResult.FixtureID
INNER JOIN Score s
ON ScoreResult.ScoreID = s.ScoreID
INNER JOIN ScoreReference sr
ON s.ScoreDifference = sr.ScoreDifference
Select * from dbo.Score
Select * from dbo.ScoreReference
SELECT * from dbo.Fixture
SELECT * From TeamWeighting
END
答案 1 :(得分:1)
试试这个。这应该工作
DECLARE @fixtureID INT,
@scoreDifference INT
;WITH cte AS
(
SELECT f.FixtureID,
ht.FinalTeamWeight - at.FinalTeamWeight AS TeamScore
FROM dbo.Fixture f
OUTER APPLY
(
SELECT tw.FinalTeamWeight
FROM dbo.TeamWeighting tw
WHERE tw.TeamID = f.HomeTeamID
) ht --HomeTeam
OUTER APPLY
(
SELECT tw.FinalTeamWeight
FROM dbo.TeamWeighting tw
WHERE tw.TeamID = f.AwayTeamID
) at --AwayTeam
WHERE f.HomeScore IS NULL AND f.FixtureDate < GETDATE()
)
SELECT FixtureID,
ScoreDifference
INTO #processData
FROM
(
SELECT DISTINCT c.FixtureID,
TeamScore,
CASE WHEN TeamScore BETWEEN sr.HomeWeighting AND sr.AwayWeighting
THEN sr.ScoreDifference
WHEN TeamScore BETWEEN sr.AwayWeighting AND sr.HomeWeighting
THEN sr.ScoreDifference
END AS ScoreDifference
FROM cte c
CROSS APPLY ScoreReference sr
) score
WHERE ScoreDifference IS NOT NULL
WHILE (SELECT COUNT(*) FROM #processData) > 0
BEGIN
SELECT TOP 1 @fixtureID = FixtureID,
@scoreDifference = ScoreDifference
FROM #processData
UPDATE f
SET f.HomeScore = score.HomeScore,
f.AwayScore = score.AwayScore
FROM
(
SELECT TOP 1 HomeScore,
AwayScore,
@fixtureID AS FixtureID
FROM Score s
WHERE ScoreDifference = @scoreDifference
ORDER BY NEWID()
) AS score
INNER JOIN Fixture f
ON f.FixtureID = score.FixtureID
DELETE #processData
WHERE FixtureID = @fixtureID
END
DROP TABLE #processData