如何计算行之间的时差

时间:2019-03-31 18:27:56

标签: sql sql-server sql-server-2008

我在表中有以下数据:

qincId   ID     lc1           lc2            Time                    SP
963     544 22.3000526428   73.1743087769   2019-03-31 17:00:46.000  15
965     544 22.2998828888   73.1746368408   2019-03-31 17:01:07.000  2
968     544 22.2998828888   73.1746368408   2019-03-31 17:01:40.000  2
997     544 22.3010215759   73.1744003296   2019-03-31 17:06:11.000  15
998     544 22.3011436462   73.1747131348   2019-03-31 17:06:21.000  17
1010    544 22.3034667969   73.1747512817   2019-03-31 17:08:04.000  0
1011    544 22.3032741547   73.1747512817   2019-03-31 17:08:03.000  0
1565    544 22.3032035828   73.1748123169   2019-03-31 18:45:26.000  0
1571    544 22.3028964996   73.1748123169   2019-03-31 18:46:03.000  16
1573    544 22.3023796082   73.1747131348   2019-03-31 18:46:21.000  15
1575    544 22.3021774292   73.1746444702   2019-03-31 18:46:37.000  0
1577    544 22.3019657135   73.1747665405   2019-03-31 18:46:50.000  15
1586    544 22.3009243011   73.1742477417   2019-03-31 18:47:33.000  5
1591    544 22.2998828888   73.1745300293   2019-03-31 18:48:19.000  5
1592    544 22.2998828888   73.1745300293   2019-03-31 18:48:28.000  5
1593    544 22.2998981476   73.1746063232   2019-03-31 18:48:29.000  4
1597    544 22.3000450134   73.1744232178   2019-03-31 18:49:08.000  0
1677    544 22.3000450134   73.1744232178   2019-03-31 19:03:28.000  0

现在我只想计算下一条记录中sp = 0到行之间的时间差。

预期输出:

qincId   ID     lc1           lc2            Time                    SP   TimeDiff (Minute)
963     544 22.3000526428   73.1743087769   2019-03-31 17:00:46.000  15     NULL
965     544 22.2998828888   73.1746368408   2019-03-31 17:01:07.000  2      NULL
968     544 22.2998828888   73.1746368408   2019-03-31 17:01:40.000  2      NULL
997     544 22.3010215759   73.1744003296   2019-03-31 17:06:11.000  15     NULL
998     544 22.3011436462   73.1747131348   2019-03-31 17:06:21.000  17     NULL
1010    544 22.3034667969   73.1747512817   2019-03-31 17:08:04.000  0       0.01 
1011    544 22.3032741547   73.1747512817   2019-03-31 17:08:03.000  0       97
1565    544 22.3032035828   73.1748123169   2019-03-31 18:45:26.000  0       1
1571    544 22.3028964996   73.1748123169   2019-03-31 18:46:03.000  16     NULL
1573    544 22.3023796082   73.1747131348   2019-03-31 18:46:21.000  15     NULL
1575    544 22.3021774292   73.1746444702   2019-03-31 18:46:37.000  0      0.21
1577    544 22.3019657135   73.1747665405   2019-03-31 18:46:50.000  15     NULL
1586    544 22.3009243011   73.1742477417   2019-03-31 18:47:33.000  5      NULL
1591    544 22.2998828888   73.1745300293   2019-03-31 18:48:19.000  5      NULL
1592    544 22.2998828888   73.1745300293   2019-03-31 18:48:28.000  5      NULL
1593    544 22.2998981476   73.1746063232   2019-03-31 18:48:29.000  4      NULL
1597    544 22.3000450134   73.1744232178   2019-03-31 18:49:08.000  0      14
1677    544 22.3000450134   73.1744232178   2019-03-31 19:03:28.000  0      NULL

所以基本上我只想以分钟为单位计算时差。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

如果在下一条记录中,您的意思是该行的最小时间大于当前时间:

import pendulum  # just to get a timestamp
from pathlib import Path
def is_cleanup_needed():
    path = "D:\Test"  # Just an example, Linux user please change this
    for folder in path.glob('**/*'):
        folder_age = folder.stat().st_ctime  # Get the time in seconds
        age_in_hours = (pendulum.now().timestamp() - folder_age) / (60 * 60)
        if age_in_hours > 12:  # replace 12 with any hour you want
            return True
    return False

答案 1 :(得分:1)

您可以尝试使用lag()sqlserver版本> = 2012

select *, case when sp=0 then
datediff(second,time,lag(time) over(order by time)) else null end
from table_name