我有不同的STime和ETime的Id1和Id2。對於每組Id1和Id2,我傾向於獲取第一個和最後一個記錄(如輸出中所示)。 我試圖通過ASC和DESC順序使用分區與ID1和ID2,並責令該表:獲取表格中的第一個和最後一個記錄
ROW_NUMBER() OVER(PARTITION BY B.HardwareId, A.TripId ORDER BY StartTime) AS first_record,
ROW_NUMBER() OVER(PARTITION BY B.HardwareId, A.TripId ORDER BY StopTime DESC) AS last_record
如預期,我沒有得到結果。
SELECT
A.Id1 AS Id1,
A.Id2 AS TriId2pId,
STime,
ETime,
Latitude,
Longitude
FROM
Tr.T1 AS A
JOIN
Tp.G2 AS B
ON
A.STime < B.DateTime
AND A.ETime >= B.DateTime
AND A.Id1 = B.Id1
WHERE
(A._PARTITIONTIME BETWEEN TIMESTAMP('2016-11-23')
AND TIMESTAMP('2016-11-23')
AND A.Id1 IN (976))
ORDER BY
B.Id1,
A.Id2,
B.DateTime
Id1 Id2 STime ETime Latitude Longitude
976 715 2016-11-23 13:06:26 2016-11-23 13:23:30 26.2230015 -80.12314
976 715 2016-11-23 13:06:26 2016-11-23 13:23:30 26.2229767 -80.12326
976 715 2016-11-23 13:06:26 2016-11-23 13:23:30 26.2226944 -80.12344
976 800 2016-11-23 13:06:26 2016-11-23 13:24:59 26.2251511 -80.11865
976 800 2016-11-23 13:06:26 2016-11-23 13:24:59 26.2251511 -80.11912
976 800 2016-11-23 13:06:26 2016-11-23 13:24:59 26.2250233 -80.11929
976 800 2016-11-23 13:06:26 2016-11-23 13:24:59 26.2245369 -80.11929
Id1 Id2 STime ETime Latitude Longitude
976 715 2016-11-23 13:06:26 2016-11-23 13:23:30 26.2230015 -80.12314
976 715 2016-11-23 13:06:26 2016-11-23 13:23:30 26.2226944 -80.12344
976 800 2016-11-23 13:06:26 2016-11-23 13:24:59 26.2251511 -80.11865
976 800 2016-11-23 13:06:26 2016-11-23 13:24:59 26.2245369 -80.11929
首先和最後一個每個Id或整個輸出? –
我正在嘗試每個Id – user3447653
以及「first」和「last」是什麼?它是基於哪個字段? –