0

这是一个有点复杂的查询,但我有一个斯诺克比赛数据库,并且正在尝试生成谁打过并赢得了最多决定性帧的统计数据(如果您不了解比赛,请不要担心规则)。

桌子:

ID player1  player2   bestOf     player1Score      player2Score
1   1           2          9          5                   0
2   2           1          9          5                   4
3   1           2          9          5                   4
4   2           1          9          4                   5

我想要做的是如下所示:

SELECT COUNT(*) AS played, DISTINCT(player1,player2) AS playerID 
FROM matches 
WHERE (player1Score=BestOf / 2 + 0.5 AND player2Score=BestOf / 2 - 0.5) 
GROUP BY playerID

上面的查询不起作用,因为我相信 DISTINCT 不支持多列。我从上表中寻找的结果是:

playerID played  won
1           3      2
2           3      1

表中的第一行不显示,因为它不是最后一帧。

我尝试了各种变化,例如:

SELECT GROUP(player1,player2)
SELECT player1 + player2 AS playerID, select DISTINCT(playerID)
SELECT (player1 + player2) AS playerID GROUP BY playerID

和其他几个。任何提示将不胜感激!

4

2 回答 2

0

我实际上并没有你的表,所以我的 sql 可能有很多语法错误。但希望我能理解这个总体思路。我们可以计算player1获胜的决胜局列表,并将其与player2获胜的决胜局列表合并。只要我们将获胜者的列名重命名为相同的字符串('player'),这些表的并集应该产生一个表,标出每个人获胜的次数。

select player, count(*) as num_won from ((select player1 as player from matches where player1Score - player2Score = 1) union (select player2 as player from matches where player2Score - player1Score = 1));

这应该为您提供从每个玩家的 id 到他们获胜的次数的映射。

然后考虑

select player, count(*) as num_played from ((select player1 as player from matches where abs(player1Score - player2Score) = 1) union (select player2 as player from matches where abs(player2Score - player1Score) = 1));

使用 abs 函数应该可以帮助您发现每个玩家玩的决定帧游戏的数量。现在我们结合这两个表来得到你的最终答案

select players_table.player, players_table.num_played, winners_table.num_won from (select player, count(*) as num_won from ((select player1 as player from matches where player1Score - player2Score = 1) union (select player2 as player from matches where player2Score - player1Score = 1)) winners_table), (select player, count(*) as num_played from ((select player1 as player from matches where abs(player1Score - player2Score) = 1) union (select player2 as player from matches where abs(player2Score - player1Score) = 1)) players_table) where winners_table.player == players_table.player;
于 2012-04-26T07:44:43.150 回答
0
create table temp_score
select playerid, count(*) as won 
FROM(
  Select   case  when player1score > player2score then  player1 else player2 end as playerid
    from score
    where
    (player2Score=Round(BestOf / 2 + 0.5,0) AND player1Score=round(BestOf / 2 - 0.5,0))
    or
    (player1Score=Round(BestOf / 2 + 0.5,0) AND player2Score=round(BestOf / 2 - 0.5,0))
) a
group by playerid

Select playerid,won, (Select SUM(won) from temp_score)  as TotalPlayed  from temp_score
于 2012-04-26T09:06:41.020 回答