我在岩石,纸张,剪刀游戏上工作,我有一张桌子可以保存圆形结果,但是我在每一轮中每个玩家节省一排,所以如果有N个球员他们每轮也会N行。
我想要的是显示一个如何进行巡视的报告,但我不想每轮显示N行,我想要显示一个显示播放器数据和N列的单行最后一栏显示获胜者的名字。
目前这是我生成的报告:
| Round | Player | Shape | Won |
|-------|-------------------|----------|-----|
| 1 | george (Player 1) | Rock | 0 |
| 1 | wendy (Player 2) | Paper | 1 |
| 2 | george (Player 1) | Rock | 0 |
| 2 | wendy (Player 2) | Rock | 0 |
| 3 | george (Player 1) | Paper | 1 |
| 3 | wendy (Player 2) | Rock | 0 |
| 4 | george (Player 1) | Scissors | 0 |
| 4 | wendy (Player 2) | Rock | 1 |
| 5 | george (Player 1) | Scissors | 0 |
| 5 | wendy (Player 2) | Scissors | 0 |
我的预期结果是这样的:
| Round | Player1Name | Player2Name | Player1Shape | Player2Shape | PlayerWinner |
|-------|-------------|-------------|--------------|--------------|------------------|
| 1 | george | wendy | Paper | Rock | Wendy (Player2) |
| 2 | george | wendy | Rock | Rock | Draw (No Winner) |
| 3 | george | wendy | Paper | Rock | George (Player1) |
| 4 | george | wendy | Scissors | Rock | Wendy (Player2) |
| 5 | george | wendy | Scissors | Scissors | Draw (No Winner) |
我不知道这种报告是否可行我不是T-SQL的专家,我使用的是SQL Server 2016.
在这里,您可以看到一个SQL Fiddle,其中包含3个与真实模型非常相似的表,其中包含测试数据和用于生成第一个报告的T-SQL代码。
我将非常感谢您的帮助
答案 0 :(得分:2)
使用条件聚合和一些字符串操作:
select
Round
, Player1Name = max(case when left(right(player,2),1)='1' then left(Player,charindex(' (',Player)) end)
, Player2Name = max(case when left(right(player,2),1)='2' then left(Player,charindex(' (',Player)) end)
, Player1Shape = max(case when left(right(player,2),1)='1' then Shape end)
, Player2Shape = max(case when left(right(player,2),1)='2' then Shape end)
, PlayerWinner = max(case when won = 1 then Player else 'Draw (NoWinner)' end)
from t
group by Round
rextester演示:http://rextester.com/DRPRZV13212
返回:
+-------+-------------+-------------+--------------+--------------+-------------------+
| Round | Player1Name | Player2Name | Player1Shape | Player2Shape | PlayerWinner |
+-------+-------------+-------------+--------------+--------------+-------------------+
| 1 | george | wendy | Rock | Paper | wendy (Player 2) |
| 2 | george | wendy | Rock | Rock | Draw (NoWinner) |
| 3 | george | wendy | Paper | Rock | george (Player 1) |
| 4 | george | wendy | Scissors | Rock | wendy (Player 2) |
| 5 | george | wendy | Scissors | Scissors | Draw (NoWinner) |
+-------+-------------+-------------+--------------+--------------+-------------------+
更新:最初sql小提琴不会加载,但一旦它加载,我将模式复制到rextester(虽然在小提琴数据中只有3轮):
select
R.RoundNumber as Round
, Player1Name = max(case when p.Number=1 then p.Name end)
, Player2Name = max(case when p.Number=2 then p.Name end)
, Player1Shape = max(case when p.Number=1 then r.PlayerShape end)
, Player2Shape = max(case when p.Number=2 then r.PlayerShape end)
, PlayerWinner = max(case when r.IsWinner = 1 then concat(p.Name, ' (Player ',p.Number,')')
else 'Draw (NoWinner)' end)
from [dbo].[Game] as G
inner join [dbo].[Player] as P
on G.Token = P.GameToken
inner join [dbo].[Round] as R
on P.Token = R.PlayerToken
where G.Token = 'A0cc2B30-6D2D-446F-9D44-58ba08F7eae5'
group by RoundNumber
order by R.RoundNumber asc
rextester演示:http://rextester.com/KUA90275
返回:
+-------+-------------+-------------+--------------+--------------+-------------------+
| Round | Player1Name | Player2Name | Player1Shape | Player2Shape | PlayerWinner |
+-------+-------------+-------------+--------------+--------------+-------------------+
| 1 | george | wendy | Rock | Paper | wendy (Player 2) |
| 2 | george | wendy | Rock | Rock | Draw (NoWinner) |
| 3 | george | wendy | Paper | Rock | george (Player 1) |
+-------+-------------+-------------+--------------+--------------+-------------------+