Skip to content
Advertisement

Query previous rows on a condition

I have a table of data about a user’s flight booking patterns on a website. Let’s assume the following data is all the historical data I have about my user.

The session_date is the day that the user came onto the website and searched a specific route, while the flight_date is the departure date of the flight. I have ordered the table by the session_date. The outcome is recorded in booked.

I would like to output a new column in my table, called previous_flight_date. The new column will state with each search, the previous booked flight_date for that specific route. Even if a user has searched that same route multiple times but never booked, the value in this column will be empty.

so for example, the column will be null until the 4th row which reflects ‘8 Mar’, as the user had booked a flight from CA–>SG, to depart on that day.

I have tried using LAST_VALUE but it didn’t work. I also do not know how I can use LAG() when I have multiple different types of routes, and I want to find previous rows on a condition. Would be great if a solution was suggested! thank you.

Advertisement

Answer

I think you can do this with first_value(). The trick is to put a condition within the window function, turn on the ignore nulls option, and then use a window frame specifications that looks back on the previous rows having the same departure/arrival, not including the current row:

Actually a window max() would work too (and then, no need for ignore nulls):

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement