Bird
0
0

Which window function would you use to calculate a moving average of sales over the last 3 days in PostgreSQL?

easy📝 Conceptual Q2 of 15
PostgreSQL - Window Functions in PostgreSQL
Which window function would you use to calculate a moving average of sales over the last 3 days in PostgreSQL?
ACOUNT(sales) OVER (PARTITION BY date)
BRANK() OVER (ORDER BY sales)
CSUM(sales) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
DMAX(sales) OVER (PARTITION BY date)
Step-by-Step Solution
Solution:
  1. Step 1: Identify moving average calculation

    Moving average sums values over a sliding window of rows, here last 3 days means current row plus 2 preceding rows.
  2. Step 2: Match window frame clause

    ROWS BETWEEN 2 PRECEDING AND CURRENT ROW defines a 3-row window for the sum.
  3. Final Answer:

    SUM(sales) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) -> Option C
  4. Quick Check:

    Moving average uses SUM() with ROWS frame [OK]
Quick Trick: Use ROWS BETWEEN for moving window sums [OK]
Common Mistakes:
  • Using PARTITION BY instead of ORDER BY for moving windows
  • Using COUNT() which counts rows, not sums values
  • Confusing RANK() with aggregation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes