简体   繁体   中英

Subtract all the rows from the value of first row in a MySQL column

This is my Select query

Select
  snpc_stats.gamedetail.Player,
  Sum(snpc_stats.gamedetail.Points + snpc_stats.gamedetail.Hits) As TotalPoints,
  COUNT(*) As 'Games Played',
  Sum(If(snpc_stats.gamedetail.Finish = 1, 1, 0)) As Wins,
  Sum(If(snpc_stats.gamedetail.Finish = 2, 1, 0)) As Second,
  Sum(If(snpc_stats.gamedetail.Finish = 3, 1, 0)) As Third,
  Round(Avg(snpc_stats.gamedetail.Finish),2) As 'Average Finish',
  Sum(snpc_stats.gamedetail.Hits) As `Total Hits`, 
  Sum(snpc_stats.gamedetail.ChampFund) As ChampFund,
  Sum(snpc_stats.games.BuyIn) + (snpc_stats.gamedetail.ChampFund) As Cost,
  Sum(snpc_stats.gamedetail.Winnings) As Winnings,
  Sum(snpc_stats.gamedetail.Winnings) - (snpc_stats.games.BuyIn) - (snpc_stats.gamedetail.ChampFund) As 'Total Winnings',
  COUNT(snpc_stats.games.Round) As round

From
  snpc_stats.gamedetail Inner Join
  snpc_stats.games On snpc_stats.games.GameID =
  snpc_stats.gamedetail.GamesID

Where
  snpc_stats.games.Season = '2015 Season'

Group By
  snpc_stats.gamedetail.Player, snpc_stats.games.Season

Order By
  TotalPoints Desc

After the TotalPoints Column I like to add a column that show the amount of points each player trails behind the leader, like the table below:

Rank | Player | Total Points | Points Behind
 1      Bill       164            -
 2       Al        152            -12
 3       Ed        151            -13
 4      Jill       123            -41
 5      Bob        121            -43
 6      Joe        102            -62
 7      Dave        82            -82
 8      Rob         60            -104
 9      Doug        60            -104
10      Don         51            -113
11      Dan         30            -134

Any help would be so appreciated!


If you are using a server side language that connects to the database and runs the query, you can preserve total points value of first row in some variable and subtracting all the following rows from it


make a sub query that returns total points of the first row and use it in the selection part of the query

 Select snpc_stats.gamedetail.Player, Sum(snpc_stats.gamedetail.Points + snpc_stats.gamedetail.Hits) As TotalPoints, (Sum(snpc_stats.gamedetail.Points + snpc_stats.gamedetail.Hits) - (Select Sum(snpc_stats.gamedetail.Points + snpc_stats.gamedetail.Hits) From snpc_stats.gamedetail Inner Join snpc_stats.games On snpc_stats.games.GameID = snpc_stats.gamedetail.GamesID Where snpc_stats.games.Season = '2015 Season' Group By snpc_stats.gamedetail.Player, snpc_stats.games.Seaso Order By Sum(snpc_stats.gamedetail.Points + snpc_stats.gamedetail.Hits) Desc Limit 1)) As Points Behind, COUNT(*) As 'Games Played', Sum(If(snpc_stats.gamedetail.Finish = 1, 1, 0)) As Wins, Sum(If(snpc_stats.gamedetail.Finish = 2, 1, 0)) As Second, Sum(If(snpc_stats.gamedetail.Finish = 3, 1, 0)) As Third, Round(Avg(snpc_stats.gamedetail.Finish),2) As 'Average Finish', Sum(snpc_stats.gamedetail.Hits) As `Total Hits`, Sum(snpc_stats.gamedetail.ChampFund) As ChampFund, Sum(snpc_stats.games.BuyIn) + (snpc_stats.gamedetail.ChampFund) As Cost, Sum(snpc_stats.gamedetail.Winnings) As Winnings, Sum(snpc_stats.gamedetail.Winnings) - (snpc_stats.games.BuyIn) - (snpc_stats.gamedetail.ChampFund) As 'Total Winnings', COUNT(snpc_stats.games.Round) As round From snpc_stats.gamedetail Inner Join snpc_stats.games On snpc_stats.games.GameID = snpc_stats.gamedetail.GamesID Where snpc_stats.games.Season = '2015 Season' Group By snpc_stats.gamedetail.Player, snpc_stats.games.Season Order By TotalPoints Desc 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM