我有一个由以下数据组成的表格:
ID status-before status-current DATE-timestamp
5 Un-Assigned Assigned 2013-12-17 14:14:04
5 Assigned Completed 2013-12-21 14:14:04
7 Un-Assigned Assigned 2014-01-02 14:14:04
7 Assigned Completed 2014-01-21 14:14:04
7 Completed Locked 2014-01-21 14:14:04
8 Un-Assigned Assigned 2014-12-21 14:14:04
8 Assigned Completed 2014-12-27 14:14:04
我使用groupby对id和排序的timestamp字段进行分组。我不知道如何使用rownum(或任何其他方法)来计算只有相同id的行的时差。
我希望我的最终输出如下
ID status-before status-current DATE-timestamp Time Diff(Hours)
5 Un-Assigned Assigned 2013-12-17 14:14:04 xx
5 Assigned Completed 2013-12-21 14:14:04 xx
7 Un-Assigned Assigned 2014-01-02 14:14:04 xx
7 Assigned Completed 2014-01-21 14:14:04 xx
7 Completed Locked 2014-01-21 14:14:04 xx
8 Un_Assigned Assigned 2014-12-21 14:14:04 xx
8 Assigned Completed 2014-12-27 14:14:04 xx
3条答案
按热度按时间3bygqnnd1#
如果您对分配和完成之间的时间差感兴趣,可以使用下面的脚本。
ygya80vv2#
大概,您想要id的下一个状态的时间差。让我假设mysql版本8之前。可以使用相关子查询获取下一个时间戳,然后使用
timestampdiff()
:如果您使用的是mysql 8.0,那么可以使用
lead()
功能。holgip5t3#
使用
TIMEDIFF
返回hours:minutes:seconds
或者DATEDIFF
返回days
```SELECT TIMEDIFF('complete.TIMESTAMP','assign.TIMESTAMP');
SELECT DATEDIFF('complete.TIMESTAMP','assign.TIMESTAMP');