连接表中特定列的每日SQL Server最大时间戳

时间:2017-05-01 23:25:16

标签: sql sql-server gtfs sql-timestamp

我对SQL很陌生,基本上用google搜索到了这一点,我现在已经陷入困境了......所以我希望那里有人可以提供帮助!

目标:获取MS SQL Server中每个trip_id每天最新时间戳的行(有多个表需要连接以获取所需数据)。

所以每个trip_id应该每天有1行,就像这样... [删除了一些列以便于阅读]

timestamp,trip_id,stop_id,stop_code,arrival_time,departure_delay
4/28/2017 18:29,8888922,2847,52818,11:02:34,0
4/27/2017 18:26,8888922,2847,52818,11:02:34,60
4/25/2017 18:27,8888922,2847,52818,11:02:34,-120
4/28/2017 18:56,8888922,2847,52818,11:32:34,-60
4/25/2017 18:59,8888922,2847,52818,11:32:34,120
4/28/2017 19:34,8888922,2847,52818,12:02:34,360
4/27/2017 19:31,8888922,2847,52818,12:02:34,540
4/25/2017 19:27,8888922,2847,52818,12:02:34,-120

但是现在我能做的最好的事情就是每天获取最大时间戳,并使用以下查询获取departure_delay

select
max(trip_updates.timestamp) as max, stop_times.trip_id, stops.stop_id, stops.stop_code, stop_times.arrival_time, trips.service_id,
stops.stop_name, stop_times.shape_dist_traveled, stop_time_updates.departure_delay
from stops
inner join stop_times on stops.stop_id = stop_times.stop_id
inner join trips on trips.trip_id = stop_times.trip_id
inner join routes on trips.route_id = routes.route_id
inner join trip_updates on stop_times.trip_id = trip_updates.trip_id
inner join stop_time_updates on trip_updates.oid = stop_time_updates.trip_update_id
where
stop_code = '52818'
and service_id = '1'
and stop_times.arrival_time between '11:00%' and '14:00%'
and route_short_name = '134'
group by stop_times.trip_id, stops.stop_id, stops.stop_code, stop_times.arrival_time, trips.service_id,
stops.stop_name, stop_times.shape_dist_traveled, stop_time_updates.departure_delay
order by stop_times.arrival_time asc, max(trip_updates.timestamp) desc

这给我的结果如...

timestamp,trip_id,stop_id,stop_code,arrival_time,departure_delay
4/28/2017 18:29,8888922,2847,52818,11:02:34,0
4/28/2017 18:21,8888922,2847,52818,11:02:34,30
4/28/2017 18:16,8888922,2847,52818,11:02:34,60
4/28/2017 18:11,8888922,2847,52818,11:02:34,120
4/27/2017 18:26,8888922,2847,52818,11:02:34,60
4/27/2017 18:22,8888922,2847,52818,11:02:34,30
4/27/2017 18:20,8888922,2847,52818,11:02:34,0

感谢所有帮助!谢谢!

2 个答案:

答案 0 :(得分:0)

正如我的评论中所提到的,如果您需要选择所有当前列,实现此目的的一种方法是使用ROW_NUMBER()窗口功能并删除GROUP BY。例如,

SELECT [Max] = [timestamp], trip_id, stop_id, stop_code, arrival_time, service_id, stop_name, shape_dist_traveled, departure_delay
FROM
(
    SELECT trip_updates.[timestamp], stop_times.trip_id, stops.stop_id, stops.stop_code, stop_times.arrival_time, 
           trips.service_id, stops.stop_name, stop_times.shape_dist_traveled, stop_time_updates.departure_delay,
           RN = ROW_NUMBER() OVER (PARTITION BY stop_times.trip_id, CAST(trip_updates.[timestamp] AS DATE) ORDER BY trip_updates.[timestamp] DESC) 
           -- This assigns a row number for each row within each trip_id and each day, where a row number of 1 will be the highest timestamp.
    FROM ... 

    <put all your current joins / where clauses here>

    AND route_short_name = '134'
    -- Note: no GROUP BY
) AS T
WHERE RN = 1 -- This ensures you select only the the first row for each trip_id.
ORDER BY arrival_time, [timestamp]; 

答案 1 :(得分:0)

您可以加入具有所需内容的子查询:

SELECT  last_trip.timestamp AS max ,
        stop_times.trip_id ,
        stops.stop_id ,
        stops.stop_code ,
        stop_times.arrival_time ,
        trips.service_id ,
        stops.stop_name ,
        stop_times.shape_dist_traveled ,
        stop_time_updates.departure_delay
FROM    stops
        INNER JOIN stop_times ON stops.stop_id = stop_times.stop_id
        INNER JOIN trips ON trips.trip_id = stop_times.trip_id
        INNER JOIN routes ON trips.route_id = routes.route_id

        -- instead of joining directly to trip_updates, join to query
        -- that will number the entries per day
        INNER JOIN ( SELECT * ,
                            ROW_NUMBER() OVER ( PARTITION BY trip_id ORDER BY timestamp DESC ) AS rownum
                     FROM   trip_updates
                   ) AS last_trip ON stop_times.trip_id = last_trip.trip_id and rownum = 1 -- and join on the latest

        INNER JOIN stop_time_updates ON trip_updates.oid = stop_time_updates.trip_update_id
WHERE   stop_code = '52818'
        AND service_id = '1'
        AND stop_times.arrival_time BETWEEN '11:00%'
                                    AND     '14:00%'
        AND route_short_name = '134'
GROUP BY stop_times.trip_id ,
        stops.stop_id ,
        stops.stop_code ,
        stop_times.arrival_time ,
        trips.service_id ,
        stops.stop_name ,
        stop_times.shape_dist_traveled ,
        stop_time_updates.departure_delay
ORDER BY stop_times.arrival_time ASC ,
        MAX(trip_updates.timestamp) DESC;