Bird
0
0

You need to compute the running total of revenue per store ordered by transaction_time and reuse this window specification in multiple queries. Which approach is best?

hard📝 Application Q8 of 15
PostgreSQL - Window Functions in PostgreSQL
You need to compute the running total of revenue per store ordered by transaction_time and reuse this window specification in multiple queries. Which approach is best?
AUse SUM() OVER (PARTITION BY store ORDER BY transaction_time) repeatedly in each query
BDefine a named window with PARTITION BY store ORDER BY transaction_time and use it in SUM() OVER w
CCreate a temporary table with cumulative sums precomputed per store
DUse GROUP BY store and ORDER BY transaction_time in the main query
Step-by-Step Solution
Solution:
  1. Step 1: Define a named window

    Use WINDOW w AS (PARTITION BY store ORDER BY transaction_time) to specify the window once.
  2. Step 2: Use the named window in aggregate

    Apply SUM(revenue) OVER w to calculate running totals efficiently.
  3. Step 3: Reuse the named window

    Named windows avoid repetition and improve query readability and maintainability.
  4. Final Answer:

    Define a named window with PARTITION BY store ORDER BY transaction_time and use it in SUM() OVER w -> Option B
  5. Quick Check:

    Named windows simplify reuse and avoid redundancy [OK]
Quick Trick: Define once, reuse named window in multiple queries [OK]
Common Mistakes:
  • Repeating window definitions instead of naming
  • Using GROUP BY instead of window functions for running totals
  • Precomputing in temp tables unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes