LogiQuest

551. Order vs Previous Order

HardWindow FunctionsLAG

For every order return id, customer_id, amount and the same customer's previous order amount as prev_amount (NULL for their first), ordered by customer_id then order_date.

customers

idnamecity
1AnilDelhi
2BinaChennai
3ChetanDelhi
4DivyaKolkata
5EshaChennai

orders

idcustomer_idamountorder_date
115002024-01-05
217002024-02-10
322002024-01-20
4315002024-03-01
528002024-03-15
613002024-03-20
7510002024-02-01

Your query runs on a fresh copy of these tables (SQLite). Column names in your result do not need to match.

Loading the MyLoginn IDE…