SQL Server中的数据分发

时间:2018-09-21 11:46:26

标签: sql-server function

在我开始研究它并意识到它比我想象的还要复杂之前,它似乎是正常的事情,任何指针都将对您有所帮助,尝试了所有排名函数和百分比函数。

我们游戏中有4个人,每个玩家开始时都有一定数量的卡(下表中的Cardcount),拥有最多卡数的两名玩家将开始玩(A和B从下表中开始)通过扔一张纸牌直到一个或多个与一张纸牌具有相同数量的纸牌游戏的玩家可以加入游戏

remote:
remote: To create a merge request for deploy/staging, visit:
remote:   https://gitlab.com/USER/PROJECT/merge_requests/new?merge_request%5Bsource_branch%5D=deploy%2Fstaging
remote:

这就是我想要的输出

Player | Cardcount | 1st Round | 2nd Round | 3rd Round
-------+-----------+-----------+-----------+-----------
  A    |     8     |    7      |    6      |   5
  B    |     3     |    2      |    1      |   0
  C    |     2     |    2      |    1      |   0
  D    |     1     |    1      |    1      |   0


Player | Round | Cards remaining | Comments
-------+-------+-----------------+-----------------------------------------------
  A    |    1  |     7           | Player A & B start - have the highest cards 
  B    |    1  |     2    
  A    |    2  |     6           | Player C joins - same number of cards as B  
  B    |    2  |     1    
  C    |    2  |     1    
  A    |    3  |     5           | Player D joins -same number of cards as B & C 
  B    |    3  |     0    
  C    |    3  |     0    
  D    |    3  |     0   

任何使用SQL函数的指针都会有所帮助

我正在尝试计算查询以预测结果。在上面的示例中,我们有4个玩家,每个玩家从一定数量的卡A(8卡),B(3卡),C(2卡)D(1卡)开始。同样,玩家数量不是固定的,上面的4个玩家只是一个例子。最多可容纳20人。

1 个答案:

答案 0 :(得分:0)

我会使用apply

select t.player, tt.rounds, tt.rounds_val as CardsRemaining
from table t cross apply
     ( values (1, [1stRound]), (2, [2ndRound]), (3,  [3rdRound]) 
     ) tt(rounds, rounds_val)
order by rounds;