我想找到每一行的差异,从第一行As Alias

时间:2015-01-23 20:11:31

标签: mysql

澄清我的标题

我想把领导者的距离制成表格,每个连续的终结者都来自第一名,如下表所示。

Finish | Points | Points Behind
   1   |  102   |
   2   |   92   |   10
   3   |   82   |   20
   4   |   71   |   31
   5   |   61   |   41
   6   |   50   |   52
   7   |   40   |   62
   8   |   30   |   72
   9   |   20   |   82
  10   |   10   |   92

 Select
  snpc_stats.gamedetail.Finish,
  snpc_stats.gamedetail.Points,
  some code I don't know As 'Points Behind'
 From
  snpc_stats.gamedetail
 Where
  snpc_stats.gamedetail.GamesID = 113

1 个答案:

答案 0 :(得分:1)

您可以从first finish获取积分,并在表格的其余部分执行cross join

<强> SQL Fiddle

select  gd.Finish, gd.Points, 
        t.Points-gd.Points as PointsBehind
from gamedetail gd
cross join ( select max(Points) from gamedetail where Finish =1) t