我有一张times
表。我想创建一个视图,它将为我提供所有传感器的主键和所有时间差异。
传感器休闲:
0. start
1-3. on track timers
4. end
示例将显示最佳:
Times table:
----------------------------------------------------------
| TimeID | RaceID | RacerID | Round | Sensor | Time |
-----------------------------------------------------------
| 1 | 1 | 1 | 1 | 0 | 100540 |
-----------------------------------------------------------
| 2 | 1 | 2 | 1 | 0 | 154297 |
-----------------------------------------------------------
| 3 | 1 | 1 | 1 | 1 | 170134 |
-----------------------------------------------------------
| 4 | 1 | 2 | 1 | 1 | 185915 |
-----------------------------------------------------------
| 5 | 1 | 1 | 1 | 2 | 200137 |
-----------------------------------------------------------
| 6 | 1 | 2 | 1 | 2 | 235103 |
-----------------------------------------------------------
| 7 | 1 | 1 | 1 | 4 | 380123 |
-----------------------------------------------------------
| 8 | 1 | 2 | 1 | 4 | 421127 |
-----------------------------------------------------------
Desired view
-----------------------------------------------------------------------
| TimeID | RaceID | RacerID | Round | Time1 | Time2 | Time3 | Total |
-----------------------------------------------------------------------
| 1 | 1 | 1 | 1 | 69594 | 99597 | null | 279583 |
-----------------------------------------------------------------------
| 1 | 1 | 2 | 1 | 31618 | 80806 | null | 266830 |
-----------------------------------------------------------------------
答案 0 :(得分:1)
如果我理解正确,只需使用一系列left join
s:
SELECT t0.timeID , t0.raceID, t0.racerID, t1.time - t0.time as 'Time 1',
t2.time - t1.time as 'Time 2', t3.time - t3.time as 'Time 3', t4.time - t0.time as 'Total'
FROM time t0 left join
time t1 on t0.racerID = t1.racerID and t1.sensor = 1 left join
time t2 on t0.racerID = t2.racerID and t2.sensor = 2 left join
time t3 on t0.racerID = t3.racerID and t3.sensor = 3 left join
time t4 on t0.racerID = t4.racerID and t4.sensor = 4
where t0.sensor = 0
答案 1 :(得分:0)
您无法动态添加列,因此您需要通过在SELECT子句中的聚合函数表达式中表示Time1来指定Time1,以及所有您想要的值,这可能无法完成你表达了与其他价值观的关系。 MIN或MAX可能接近,但“第二个”或“第二个”值无法表达,因为它意味着与单独聚合的其他值的关系。
你可以考虑GROUP_CONCAT,它可能最接近你想要的。