Sample Header Ad - 728x90

Update row data with dynamic lag

0 votes
1 answer
204 views
I have a table of items and restaurants. We assume that if the restaurant entry is missing, it has to be completed with the last non-null restaurant value in the previous rows (as defined by ascending id values). CREATE TABLE food_items ( id SERIAL PRIMARY KEY, item VARCHAR(255) NOT NULL, restaurant VARCHAR(255) ); INSERT INTO food_items(item, restaurant) VALUES ('Salad','McDonalds'), ('Burger',''),('Fries',''),('Salad','Taco Bell'),('Quesadilla',''); The tricky part is that the number of rows for each restaurant is variable, so it cannot be captured with a fixed lag.
Asked by eddie (101 rep)
Jan 2, 2024, 08:17 AM
Last activity: Jun 23, 2025, 09:03 AM