Correct Running Total
Question:
The cats must be ordered by weight descending and will enter an elevator one by one. We would like to know what the running total weight is. If two cats have the same weight they must enter separately
Return: name, running total weight
Order by: weight desc
Show Table
Cats:
| name | varchar |
| breed | varchar |
| weight | float |
| color | varchar |
| age | int |
Show Desired Output
Desired output:
| name | running_total_weight |
| Smokey | 6.10 |
| Oscar | 12.20 |
| Misty | 17.90 |
| Alfie | 23.40 |
| Millie | 28.80 |
| Puss | 33.90 |
| Felix | 38.90 |
| Smudge | 43.80 |
| Charlie | 48.60 |
| Ashes | 53.10 |
| Molly | 57.30 |
| Tigger | 61.10 |
Query Window:
Correct output but can you use 'unbounded preceding'?
×
Good work!
Use UNBOUNDED PRECEDING to ensure that the running total includes all rows up to and including the current row, starting from the first row in the partition. next question
Show Answer
Answer:
select name,
sum(weight) over (order by weight DESC ROWS between unbounded preceding and current row) as running_total_weight
from cats order by running_total_weight