Bird
0
0

How can you modify a running total query to exclude rows where the amount is zero, but still keep the running total correct for the included rows?

hard📝 Application Q9 of 15
SQL - Advanced Window Functions
How can you modify a running total query to exclude rows where the amount is zero, but still keep the running total correct for the included rows?
AUse PARTITION BY amount to separate zero and non-zero amounts.
BUse a WHERE clause to filter out zero amounts before applying SUM() OVER.
CUse a CASE inside SUM() to count only amounts > 0 within the window.
DUse HAVING amount > 0 after the window function.
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering effect on running total

    Filtering rows before window function changes row order and count, breaking running total.
  2. Step 2: Use CASE inside SUM() to include only positive amounts

    This keeps all rows but sums only amounts > 0, preserving running total order.
  3. Final Answer:

    Use a CASE inside SUM() to count only amounts > 0 within the window. -> Option C
  4. Quick Check:

    CASE inside SUM() filters values without dropping rows [OK]
Quick Trick: Use CASE inside SUM() to exclude zero amounts in running total [OK]
Common Mistakes:
  • Filtering rows before window function
  • Using PARTITION BY amount incorrectly
  • Applying HAVING after window function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes