简体   繁体   中英

Add column to SQL Table based on other rows in the table

I have a table that contains stop times for a transit system. The details aren't important, but my table essentially looks like this:

我的表格列示例

I am importing the data from a CSV file which contains everything except the next stop ID. I want to generate Next Stop ID to speed up some data processing I am going to do in my app.

For each row, the Next Stop ID should be the Stop ID from the next row with matching Trip ID and Service ID. The ordering should be based on the Stop Sequence, which will be increasing but not necessarily in order (1, 20, 21, 23, etc rather than 1,2,3,4...).

Here is an example of what I'm hoping it will look like. For simplicity, I kept all the service IDs the same and there are two Trip IDs. If there is no next stop I want that entry to just be blank.

表格显示我希望完成的事情

I think it makes sense to do this entirely in SQL, but I'm not sure how best to do it. I know how I would do it in a standard programming language, but not SQL. Thank you for your help.

You can use lead() :

select 
    t.*,
    lead(stop_id) 
        over(partition by trip_id, service_id order by stop_sequence) next_stop_id
from mytable t

It is not ncessarily an good idea to actally store that derived information, since you can compute on the fly when needed (you can put the query in a view to make it easier to access it). But if you want this in an update , then, assuming that stop_id is the primary key of the table, that would look like:

update mytable 
set next_stop_id = t.next_stop_id
from (
    select 
        stop_id, 
        lead(stop_id) over(partition by trip_id, service_id order by stop_id) next_stop_id
    from mytable
) t
where mytable.stop_id = t.stop_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM