我想使用查找表而不是在我的case语句中使用硬编码值来确定设置边界。我还没有完成任何类型的查找表,所以只需要指导如何编写以下内容。
我有一张名为' ScoreReference'显示以下内容:
如何在CASE语句中使用此查找表来替换硬编码值?
SELECT
TeamScore,
CASE WHEN TeamScore BETWEEN 11 AND 25 --Home Team Wins by 1 goal
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 1 ORDER BY NEWID())
WHEN TeamScore BETWEEN 26 AND 40 --Home Team Wins by 2 goals
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 2 ORDER BY NEWID())
WHEN TeamScore BETWEEN 41 AND 55 --Home Team Wins by 3 goals
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 3 ORDER BY NEWID())
WHEN TeamScore BETWEEN 56 AND 70 --Home Team Wins by 4 goals
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 4 ORDER BY NEWID())
WHEN TeamScore > 71 --Home Team Wins by 5 goals
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 5 ORDER BY NEWID())
WHEN TeamScore BETWEEN -25 AND -11 --Away Team Wins by 1 goal
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -1 ORDER BY NEWID())
WHEN TeamScore BETWEEN -40 AND -26 --Away Team Wins by 2 goals
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -2 ORDER BY NEWID())
WHEN TeamScore BETWEEN -55 AND -41 --Away Team Wins by 3 goals
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -3 ORDER BY NEWID())
WHEN TeamScore BETWEEN -70 AND -56 --Away Team Wins by 4 goals
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -4 ORDER BY NEWID())
WHEN TeamScore <= -71 --Away Team Wins by 5 goals
THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -5 ORDER BY NEWID())
ELSE (SELECT TOP 1 ScoreID FROM dbo.Score WHERE ScoreDifference = 0 ORDER BY NEWID()) -- Draw
END AS ScoreID
更新
以下是&#39;分数&#39;表:
你可以看到我要做的是取决于每支球队的加权(一支球队加权是主队,另一支球队是客队),加权的差异反映在分数。例如,如果两队之间的权重是11到25之间的差异,那么得分差异是“1”,这意味着来自“得分”&#39;我们会随机选择一个差异为&#39; 1&#39;
的分数。表格的日期设定:
ScoreReference:
CREATE TABLE [dbo].[ScoreReference]
(
[ScoreReferenceID] TINYINT IDENTITY (1,1) NOT NULL PRIMARY KEY,
[HomeWeighting] INT NULL,
[AwayWeighting] INT NULL,
[ScoreDifference] INT NULL
)
分:
CREATE TABLE [dbo].[Score]
(
[ScoreID] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[HomeScore] INT NULL,
[AwayScore] INT NULL,
[ScoreDifference] INT NULL
)
夹具:
CREATE TABLE [dbo].[Fixture](
[FixtureID] INT NOT NULL PRIMARY KEY,
[WeekNumber] TINYINT NOT NULL,
[FixtureDate] DATE NULL,
[HomeTeamID] TINYINT NULL,
[HomeScore] INT NULL,
[AwayTeamID] TINYINT NULL,
[AwayScore] INT NULL,
[HomeTeamResult] CHAR(1) NULL,
[AwayTeamResult] CHAR(1) NULL,
[LeagueID] TINYINT CONSTRAINT FK_Fixture_League FOREIGN KEY REFERENCES League(LeagueID)
)
TeamWeighting:
WITH pl AS (SELECT DISTINCT p.PlayerID
,p.Position
,CASE WHEN p.TeamID = 0 THEN 0
ELSE p.playerWeighting END AS playerWeighting
,ABS(CHECKSUM(NewID())) % 10 + 1 AS Form
,t.TeamID
FROM dbo.Fixture f
INNER JOIN dbo.League l ON f.LeagueID = l.LeagueID
INNER JOIN dbo.Team t ON l.LeagueID = t.LeagueID
INNER JOIN dbo.Player p ON t.TeamID = p.TeamID
WHERE f.WeekNumber = 1)
,po AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY pl.TeamID, pl.Position ORDER BY NEWID()) AS Rnk FROM pl)
SELECT TeamID
,SUM(po.playerWeighting) AS TeamWeight
,Form--ABS(CHECKSUM(NewID())) % 10 + 1 AS Form
,SUM(po.playerWeighting / 10 * Form + po.playerWeighting) AS FinalTeamWeight
FROM po
WHERE (po.Position = 'GK' and po.rnk = 1) OR
(po.Position = 'DF' and po.rnk <= 4) OR
(po.Position = 'MF' and po.rnk <= 4) OR
(po.Position = 'FW' and po.rnk <= 2)
GROUP BY po.TeamID
以下是TeamScore:
;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()
)
最新代码:
;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
CASE
WHEN c.TeamScore BETWEEN HomeWeighting AND AwayWeighting AND HomeScore > AwayScore AND s.ScoreDifference = Sr.ScoreDifference
THEN S.ScoreID
WHEN c.TeamScore BETWEEN AwayWeighting AND HomeWeighting AND HomeScore < AwayScore AND s.ScoreDifference = Sr.ScoreDifference
THEN S.ScoreID
WHEN c.TeamScore BETWEEN HomeWeighting AND AwayWeighting AND HomeScore = AwayScore AND s.ScoreDifference = Sr.ScoreDifference
THEN S.ScoreID
END
--CASE WHEN TeamScore BETWEEN 11 AND 25 --Home Team Wins by 1 goal
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 1 ORDER BY NEWID())
-- WHEN TeamScore BETWEEN 26 AND 40 --Home Team Wins by 2 goals
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 2 ORDER BY NEWID())
-- WHEN TeamScore BETWEEN 41 AND 55 --Home Team Wins by 3 goals
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 3 ORDER BY NEWID())
-- WHEN TeamScore BETWEEN 56 AND 70 --Home Team Wins by 4 goals
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 4 ORDER BY NEWID())
-- WHEN TeamScore > 71 --Home Team Wins by 5 goals
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore > AwayScore AND ScoreDifference = 5 ORDER BY NEWID())
-- WHEN TeamScore BETWEEN -25 AND -11 --Away Team Wins by 1 goal
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -1 ORDER BY NEWID())
-- WHEN TeamScore BETWEEN -40 AND -26 --Away Team Wins by 2 goals
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -2 ORDER BY NEWID())
-- WHEN TeamScore BETWEEN -55 AND -41 --Away Team Wins by 3 goals
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -3 ORDER BY NEWID())
-- WHEN TeamScore BETWEEN -70 AND -56 --Away Team Wins by 4 goals
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -4 ORDER BY NEWID())
-- WHEN TeamScore <= -71 --Away Team Wins by 5 goals
-- THEN (SELECT TOP 1 ScoreID FROM dbo.Score WHERE HomeScore < AwayScore AND ScoreDifference = -5 ORDER BY NEWID())
-- ELSE (SELECT TOP 1 ScoreID FROM dbo.Score WHERE ScoreDifference = 0 ORDER BY NEWID()) -- Draw
FROM dbo.Score s
LEFT JOIN dbo.ScoreReference sr
ON s.sr.ScoreReferenceId = sr.ScoreReferenceId
)
FROM cte c
) AS ScoreResult
ON f.FixtureID = ScoreResult.FixtureID
--INNER JOIN Score s
-- ON score.ScoreID = s.ScoreID
预期输出:
答案 0 :(得分:0)
我并不完全确定你想要完成什么,但是如果保证每个结果都有ScoreDifference匹配,那么下面的内容可能会有所帮助。
select *
from (
select *,
DiffScore = HomeScore - AwayScore
from Score) s
join ScoreReference sr
on s.DiffScore = sr.ScoreDifference;
或许,您可以提供一个更具体的示例数据集示例吗?
答案 1 :(得分:0)
你可以尝试一下这方面的事情。因为我没有数据,有些表无法检查出来。但尝试这几个调整,让我知道。
SELECT
c.TeamScore
,( SELECT
CASE
WHEN c.TeamScore BETWEEN HomeWeighting AND AwayWeighting AND HomeScore > AwayScore AND S.ScoreDifference = SF.ScoreDifference
THEN S.ScoreID
WHEN c.TeamScore BETWEEN AwayWeighting AND HomeWeighting AND HomeScore < AwayScore AND S.ScoreDifference = SF.ScoreDifference
THEN S.ScoreID
END
FROM dbo.Score S
LEFT JOIN dbo.ScoreReference SF
ON S.SF.ScorereferenceId = SF.ScorereferenceId
)
FROM cte c