Search code examples
sqldatabasepostgresqlsql-updatewindow-functions

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:

example of my table columns

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.

table showing what I hope to accomplish

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.


Solution

  • 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