这是我的数据表 SQl Fiddle
它跟踪汽车表(我的事情列名是描述性的) 我每隔30秒就得到一辆车 现在我想要一份报告来获取Trip-Idle Detail
这是我的最后一次尝试
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 :(得分:0)
这里我是如何解决我的问题的 我会将其标记为任何寻找相同情况的人的答案
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