给定参与游戏的玩家,根据总得分变化对玩家组合进行排名

时间:2018-08-22 21:18:15

标签: sql

玩家可以组合成我所说的小队来玩游戏。它们通过Lineup表连接到Games表。一个小队的球员人数可以从1-5人变化。我想创建一个排行榜的排行榜,但是这个查询超出了我。

  • 我想排除1人小队
  • 我对排名标准没有强烈的看法,但是到目前为止,我得出的最好结论是elo_change字段的总和。输队的球员都将得到负elo_change,而输队的球员将得到积极的变化。如果基于其他标准(例如获胜次数)的排名比较容易,那就可以了。

编辑此外,如果可以进行查询,则可以对此模式进行更改。 FWIW,我目前的后端是sqlite,如果我转向更强大的功能,则可能是postgresql。

表结构为:

CREATE TABLE IF NOT EXISTS "team" (
  "id" integer NOT NULL PRIMARY KEY,
  "teamname" varchar(255) NOT NULL COLLATE nocase
);

CREATE TABLE IF NOT EXISTS "game" (
  "id" integer NOT NULL PRIMARY KEY,
  "winner_id" integer,
  "loser_id" integer,
  "home_team_id" integer NOT NULL,
  "away_team_id" integer NOT NULL,
  "team_size" integer NOT NULL,
  "is_completed" integer NOT NULL,
  "timestamp" datetime NOT NULL,
  FOREIGN KEY ("winner_id") REFERENCES "team" ("id"),
  FOREIGN KEY ("loser_id") REFERENCES "team" ("id"),
  FOREIGN KEY ("home_team_id") REFERENCES "team" ("id"),
  FOREIGN KEY ("away_team_id") REFERENCES "team" ("id")
);

CREATE TABLE IF NOT EXISTS "player" (
  "id" integer NOT NULL PRIMARY KEY,
  "name" varchar(255) NOT NULL COLLATE nocase,
  "elo" integer NOT NULL,
  "team_id" integer,
  FOREIGN KEY ("team_id") REFERENCES "team" ("id")
);

CREATE TABLE IF NOT EXISTS "lineup" (
  "id" integer NOT NULL PRIMARY KEY,
  "game_id" integer NOT NULL,
  "player_id" integer NOT NULL,
  "team_id" integer NOT NULL,
  "elo_change" integer NOT NULL,
  FOREIGN KEY ("game_id") REFERENCES "game" ("id"),
  FOREIGN KEY ("player_id") REFERENCES "player" ("id"),
  FOREIGN KEY ("team_id") REFERENCES "team" ("id")
);

1 个答案:

答案 0 :(得分:0)

您的数据模型中的“小队”是“团队”吗?如果这样的话,算上赢与输就足够了吗?在“ pseudo” mysql查询下,汇总了一个团队的得失(语法可能会因所使用的数据库而异)。

select team.teamname, 
       sum(CASE WHEN team.id = game.winner_id THEN 1 ELSE 0 END) as wins, 
       sum(CASE WHEN team.id = game.loser_id THEN 1 ELSE 0 END) as losses
   from team, game
where (team.id = game.winner_id or team.id = game.loser_id)
  and game.is_completed = 1
  and game.team_size > 1
group by team.teamname
order by wins desc, losses asc