Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SQL Consecutive Days Logic

vijzApr 16 2020 — edited Apr 17 2020

If no customers visit a restaurant for more than 3 days, then that restaurant will serve a new type of food starting when the next customer visits.

All restaurants serve food in the same order. Figure out which customer ate which food.

Sample Data:

Restaurant:

restaurant_idcustomer_idvisit_date
112020-01-01
212020-01-01
122020-01-03
312020-01-04
222020-01-14
312020-01-11
232020-01-14

Food:

food_idfood_name
1Spaghetti
2Chicken and Rice
3Tacos

Desired:

RestaurantCustomerVisitFood
112020-01-01Spaghetti
212020-01-01Spaghetti
122020-01-03Spaghetti
312020-01-04Spaghetti
222020-01-10Chicken and Rice
312020-01-11Chicken and Rice
232020-01-14Tacos

Not sure whether to use CTE or row_number() over (partition by )

Comments
Post Details
Added on Apr 16 2020
7 comments
625 views