Amazon Order Status
HardAmazon is a global e-commerce company that allows vendors to sell their products online to customers. Customers can order products and track their orders' status, such as 'Pending', 'Shipped', 'Delivered', etc.
You're given a table, orders, with the following columns:
order_id(integer): a unique identifier for each orderorder_date(date): the date the order status was updatedstatus(string): the status of the order, e.g., 'Pending', 'Shipped', 'Delivered', etc.
Write a SQL query that returns a table with the order_id, status, start_date, and end_date for each status period of a particular order. If a status is the first for that order, then the end_date should be NULL."
-
Common Table Expression (CTE) -
StatusChanges: We employ a CTE namedStatusChangesto prepare an intermediary result. CTEs allow us to create temporary result sets which can simplify the structure of our main query. -
Window Functions (
LEADandLAG):- We use
LEADto peek at theorder_dateof the next row in the sequence (essentially the date of the next status). This helps us determine theend_datefor a given status. - Similarly,
LAGis used to look at the previous row'sstatus, allowing us to compare the current status with its predecessor and determine if there's been a change.
Both
LEADandLAGare Window Functions, which means they operate over a set of table rows that are somehow related to the current row. This relation is established through thePARTITION BYandORDER BYclauses. - We use
-
Filtering Strategy in
WHEREClause: We use a condition that checks if the current status differs from the previous status (status != prev_status) or if the previous status is simply nonexistent (prev_status IS NULL). This ensures that we only capture rows representing genuine status changes or the initial status for an order. This step is crucial to distill the essence of our objective.