Use window functions to compute the previous order value, city rank, and percent rank in a single query. Partition by user_id for the lag, and by city for the ranking functions, ordering appropriately. Ensure the final output includes all required columns and handles NULLs correctly.
Pro tip: Always clarify the ordering for 'previous order' (e.g., by order date) and whether ties in rank should use RANK or DENSE_RANK; also mention that PERCENT_RANK returns a value between 0 and 1, which might need formatting for business users.
Determine that LAG is needed for previous order value, RANK for city rank, and PERCENT_RANK for percent rank. Specify the partitions and orderings for each.
For LAG, partition by user_id and order by order_date (or order_id as tiebreaker). For RANK and PERCENT_RANK, partition by city and order by order_value descending.
Construct a SELECT statement with the original columns plus the three window functions. Use OVER clauses with appropriate PARTITION BY and ORDER BY.
Check that the first order per user returns NULL for previous value, and that ranking functions handle ties correctly. Consider if any filters or additional ordering are needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.