从下表中,我试图找到每个Id1的开始时间和停止时间。 例如,对于Id1 = 103,它开始于2016-11-01 10:50:28 UTC,同一天车辆的停止时间为2016-11-01 11:23:55 UTC。 相似的id1 = 103,在2016-12-01,它开始于2016-12-01 10:51:28 UTC并于2016-12-01 11:28:55 UTC结束。
Id1 Id2 StartTime StartDate StopTime StopDate DateTime
1 103 314 2016-11-01 10:50:28 UTC 2016-11-01 2016-11-01 11:22:35 UTC 2016-11-01 2016-11-01 10:50:32 UTC
2 103 315 2016-11-01 10:50:28 UTC 2016-11-01 2016-11-01 11:22:48 UTC 2016-11-01 2016-11-01 10:50:49 UTC
3 103 316 2016-11-01 10:50:28 UTC 2016-11-01 2016-11-01 11:22:55 UTC 2016-11-01 2016-11-01 10:50:54 UTC
4 103 317 2016-11-01 10:50:28 UTC 2016-11-01 2016-11-01 11:23:55 UTC 2016-11-01 2016-11-01 10:51:03 UTC
5 103 318 2016-12-01 10:51:28 UTC 2016-12-01 2016-12-01 11:24:15 UTC 2016-12-01 2016-12-01 10:51:19 UTC
6 103 319 2016-12-01 10:52:28 UTC 2016-12-01 2016-12-01 11:25:55 UTC 2016-12-01 2016-12-01 10:51:24 UTC
7 103 320 2016-12-01 10:53:28 UTC 2016-12-01 2016-12-01 11:26:55 UTC 2016-12-01 2016-12-01 10:51:31 UTC
8 103 321 2016-12-01 10:54:28 UTC 2016-12-01 2016-12-01 11:27:55 UTC 2016-12-01 2016-12-01 10:51:40 UTC
9 103 322 2016-12-01 10:55:28 UTC 2016-12-01 2016-12-01 11:28:55 UTC 2016-12-01 2016-12-01 10:51:43 UTC
Output:
Id1 StartTime StartDate StopTime StopDate
1 103 2016-11-01 10:50:28 UTC 2016-11-01 2016-11-01 11:23:55 UTC 2016-11-01
5 103 2016-12-01 10:51:28 UTC 2016-12-01 2016-12-01 11:28:55 UTC 2016-12-01
我尝试使用以下查询,但不确定它为什么不起作用:
SELECT Id1, Date, StartTime, StopTime
FROM(
SELECT
Id1,
DATE(DateTime) AS Date,
FIRST_VALUE(StartTime) OVER (PARTITION BY Id1, DATE(DateTime) ORDER BY DateTime) AS StartTime,
LAST_VALUE(StopTime) OVER (PARTITION BY Id1, DATE(DateTime) ORDER BY DateTime ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS StopTime,
FROM
TestSridevi.ET_Time_Leaving)
GROUP BY Id1, Date, StartTime, StopTime
ORDER BY Id1
答案 0 :(得分:1)
尝试以下
#standardSQL
WITH yourTable AS (
SELECT 103 AS Id1, 314 AS Id2, '2016-11-01 10:50:28 UTC' AS StartTime, '2016-11-01' AS StartDate, '2016-11-01 11:22:35 UTC' AS StopTime, '2016-11-01' AS StopDate, '2016-11-01 10:50:32 UTC' AS DateTime UNION ALL
SELECT 103, 315, '2016-11-01 10:50:28 UTC', '2016-11-01', '2016-11-01 11:22:48 UTC', '2016-11-01', '2016-11-01 10:50:49 UTC' UNION ALL
SELECT 103, 316, '2016-11-01 10:50:28 UTC', '2016-11-01', '2016-11-01 11:22:55 UTC', '2016-11-01', '2016-11-01 10:50:54 UTC' UNION ALL
SELECT 103, 317, '2016-11-01 10:50:28 UTC', '2016-11-01', '2016-11-01 11:23:55 UTC', '2016-11-01', '2016-11-01 10:51:03 UTC' UNION ALL
SELECT 103, 318, '2016-12-01 10:51:28 UTC', '2016-12-01', '2016-12-01 11:24:15 UTC', '2016-12-01', '2016-12-01 10:51:19 UTC' UNION ALL
SELECT 103, 319, '2016-12-01 10:52:28 UTC', '2016-12-01', '2016-12-01 11:25:55 UTC', '2016-12-01', '2016-12-01 10:51:24 UTC' UNION ALL
SELECT 103, 320, '2016-12-01 10:53:28 UTC', '2016-12-01', '2016-12-01 11:26:55 UTC', '2016-12-01', '2016-12-01 10:51:31 UTC' UNION ALL
SELECT 103, 321, '2016-12-01 10:54:28 UTC', '2016-12-01', '2016-12-01 11:27:55 UTC', '2016-12-01', '2016-12-01 10:51:40 UTC' UNION ALL
SELECT 103, 322, '2016-12-01 10:55:28 UTC', '2016-12-01', '2016-12-01 11:28:55 UTC', '2016-12-01', '2016-12-01 10:51:43 UTC'
)
SELECT
Id1,
MIN(StartTime) AS StartTime,
StartDate,
MAX(StopTime) AS StopTime,
StopDate
FROM yourTable
GROUP BY Id1, StartDate, StopDate
ORDER BY Id1, StartDate, StopDate
结果是
Id1 StartTime StartDate StopTime stopDate
103 2016-11-01 10:50:28 UTC 2016-11-01 2016-11-01 11:23:55 UTC 2016-11-01
103 2016-12-01 10:51:28 UTC 2016-12-01 2016-12-01 11:28:55 UTC 2016-12-01