SQL分组GPS旅行数据

时间:2017-11-14 14:07:10

标签: sql-server sql-server-2014

这是我的表 SQL Fiddle

我需要创建此报告

Report

驾驶旗是汽车的状态 所以我需要真正(驾驶)的第一点作为旅行开始 最后一点作为总周期和总距离的结束

与停止相同 以及动作的最大和平均速度 表应按id排序,以保持驾驶记录的顺序 谢谢

编辑

    SELECT        
    carid
    , (SUM(CASE WHEN speed < 3 THEN 0 ELSE DATEDIFF(minute, b.trackold, b.TrackTime) END)) AS speeding
    , (SUM(CASE WHEN speed >= 3 THEN 0 ELSE DATEDIFF(minute, b.trackold, b.TrackTime) END)) AS parked
    , round(sum(Distance / 1000), 2) AS Distance
    , TrackDay 
FROM            
(
    SELECT        
        carid
        , TrackDay
        , TrackTime
        , trackold
        , speed
        , TrackDayOld
        , diff
        , Distance  
    FROM            
    (
        SELECT        
            carid
            , TrackTime
            , LAG(TrackTime, 1, NULL) OVER (PARTITION BY carid ORDER BY TrackTime) AS trackold
            , CONVERT(date, TrackTime) AS TrackDay
            , CONVERT(date, LAG(TrackTime, 1, NULL) OVER (PARTITION BY carid  ORDER BY TrackTime)) AS TrackDayOld
            , speed
            , datediff(minute, LAG(TrackTime, 1, NULL) OVER (PARTITION BY carid ORDER BY carid), TrackTime) AS diff
            , Distance 
        FROM T_Tracking
    ) a 
    WHERE a.TrackDay = a.TrackDayOld
) b GROUP BY carid
    , TrackDay

这是我的最后一次尝试(在根据速度创建驱动器标志之前)` 但它整天都在分组(得到总数)

0 个答案:

没有答案