Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Simple OVER()
PARTITION BY
Ranking functions
Window Frames
19. Window Frames – Review and Exercise 1
Analytic Functions
PARTITION BY ORDER BY
Order of Evaluation
Finished!

Instruction

Good job! Let's continue the adventure. In Part 5, we learned about window frames. Let's take a look at the following example:

SELECT
  Id,
  PaymentDate,
  PaymentAmount,
  SUM(PaymentAmount) OVER(ORDER BY PaymentDate ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotalAmount
FROM Subscription;

In the above query, we calculate a running total paid for all subscriptions up to the current row.

Exercise

For each single rental, show the Id, the RentalDate, the PaymentAmount, and the running total of PaymentAmounts for all rentals, from the oldest one (in terms of RentalDate) to the current row. Name this column RunningTotalAmount.

Stuck? Here's a hint!

Use:

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW