MYSQL Diff From MAX -
i'm trying query motogp result here's sqlfiddle
i want add 2 more columns
- pos (1, 2, 3, 4, ...)
- gap (rider laptime - 1st pos laptime)
desired result:
- 1 | marquez | 2799.627 | 2799.627
- 2 | rossi |2803.143 | 3.516
my current query:
select `rider`, sum(laptime), count(`lapno`), max(`topspeed`) ts (select `lapno`,`rider`, (t1+t2+t3+t4) laptime , `topspeed` `a_lap_time` t_laptime) group a.rider order count(`lapno`) desc, sum(laptime) asc
please advise, thank you
this result
| rider | laptime | gap | lapno | ts | |-----------------|----------|--------|-------|-------| | marc marquez | 2799.627 | 0 | 30 | 329.3 | | valentino rossi | 2803.143 | 3.516 | 30 | 319.7 |
produced by:
select `rider`, laptime, laptime - minlaptime gap, lapno, ts ( select `rider`, laptime, lapno, ts, @lap := if(@lap = 0.0, laptime, @lap) minlaptime ( select `rider`, sum(laptime) laptime , count(`lapno`) lapno , max(`topspeed`) ts (select `lapno`,`rider`, (t1+t2+t3+t4) laptime , `topspeed` `a_lap_time` t_laptime ) group a.rider ) b cross join (select @lap :=0) var order `lapno` desc, laptime asc ) c
a variable: @lap "attached" each row using cross join, using order minimum overall laptime pass value @lap. once done have 2799.627 value on each row , can calculate gap.
see sqlfiddle: http://sqlfiddle.com/#!9/bd71f/6
for pos well:
select `rider`, laptime, laptime - minlaptime gap, lapno, ts, pos ( select `rider`, laptime, lapno, ts , @lap := if(@lap = 0.0, laptime, @lap) minlaptime , @pos := @pos + 1 pos ( select `rider`, sum(laptime) laptime , count(`lapno`) lapno , max(`topspeed`) ts (select `lapno`,`rider`, (t1+t2+t3+t4) laptime , `topspeed` `a_lap_time` t_laptime ) group a.rider ) b cross join (select @lap :=0, @pos := 0) var order `lapno` desc, laptime asc ) c
result:
| rider | laptime | gap | lapno | ts | pos | |-----------------|----------|-------|-------|-------|-----| | marc marquez | 2799.627 | 0 | 30 | 329.3 | 1 | | valentino rossi | 2803.143 | 3.516 | 30 | 319.7 | 2 |
Comments
Post a Comment