SQLite - 获得每场比赛的获胜者(排名超过分区)
SQLite - Get Winner of Each Game (Rank Over Partition )
我正在为 Carcassonne 创建一个分数跟踪应用程序。我想获得每场比赛的获胜者。
我已经在 MS-SQL 中使用了它,现在我正在尝试将数据库转换为 SQLite。
架构
http://sqlfiddle.com/#!7/e8340
MS-SQL
CREATE VIEW vTopScorePerGame AS
WITH ranked AS (
SELECT RANK() OVER (PARTITION BY [GameID] ORDER BY TotalScore DESC) AS Position,
PlayerID,
PlayerName,
TotalScore,
[GameID]
FROM vScore
)
SELECT [GameID],
PlayerID,
PlayerName AS Winner,
TotalScore
FROM ranked
WHERE ranked.Position = 1
当我在 SQLFiddle 或 SQLiteBrowser 中尝试时,出现以下错误:
near "(": syntax error:
有人能指出我正确的方向吗?
函数
与
https://www.sqlite.org/lang_with.html
合并
https://www.sqlite.org/lang_corefunc.html
不支持
http://www2.sqlite.org/cvstrac/wiki?p=UnsupportedSqlAnalyticalFunctions
如果您有 SQLite 3.7.11 或更高版本,您可以简单地使用 MAX() 从组中具有最大值的行中获取值:
SELECT GameID,
PlayerID,
PlayerName AS Winner,
MAX(TotalScore) AS TotalScore
FROM vScore
GROUP BY GameID;
在早期版本中,您可以在单独的子查询中获取得分最大的球员,然后将他们与原始 table:
合并
SELECT GameID,
PlayerID,
PlayerName AS Winner,
TotalScore
FROM vScore
JOIN (SELECT GameID,
(SELECT PlayerID
FROM vScore AS s2
WHERE s2.GameID = GameIDs.GameID
ORDER BY TotalScore DESC
LIMIT 1
) AS PlayerID
FROM (SELECT DISTINCT GameID
FROM vScore) AS GameIDs)
USING (GameID, PlayerID);
我正在为 Carcassonne 创建一个分数跟踪应用程序。我想获得每场比赛的获胜者。
我已经在 MS-SQL 中使用了它,现在我正在尝试将数据库转换为 SQLite。
架构
http://sqlfiddle.com/#!7/e8340
MS-SQL
CREATE VIEW vTopScorePerGame AS
WITH ranked AS (
SELECT RANK() OVER (PARTITION BY [GameID] ORDER BY TotalScore DESC) AS Position,
PlayerID,
PlayerName,
TotalScore,
[GameID]
FROM vScore
)
SELECT [GameID],
PlayerID,
PlayerName AS Winner,
TotalScore
FROM ranked
WHERE ranked.Position = 1
当我在 SQLFiddle 或 SQLiteBrowser 中尝试时,出现以下错误:
near "(": syntax error:
有人能指出我正确的方向吗?
函数
与
https://www.sqlite.org/lang_with.html
合并
https://www.sqlite.org/lang_corefunc.html
不支持
http://www2.sqlite.org/cvstrac/wiki?p=UnsupportedSqlAnalyticalFunctions
如果您有 SQLite 3.7.11 或更高版本,您可以简单地使用 MAX() 从组中具有最大值的行中获取值:
SELECT GameID,
PlayerID,
PlayerName AS Winner,
MAX(TotalScore) AS TotalScore
FROM vScore
GROUP BY GameID;
在早期版本中,您可以在单独的子查询中获取得分最大的球员,然后将他们与原始 table:
合并SELECT GameID,
PlayerID,
PlayerName AS Winner,
TotalScore
FROM vScore
JOIN (SELECT GameID,
(SELECT PlayerID
FROM vScore AS s2
WHERE s2.GameID = GameIDs.GameID
ORDER BY TotalScore DESC
LIMIT 1
) AS PlayerID
FROM (SELECT DISTINCT GameID
FROM vScore) AS GameIDs)
USING (GameID, PlayerID);