Correct Running Total
Question:
The stocks must be ordered by their portfolio weight in descending order and will be added to the portfolio one by one. We would like to know what the running total portfolio weight is.
Return: name, running total weight
Order by: weight desc
Show Table
Dividends:
stock | varchar |
sector | varchar |
dividend | float |
frequency | varchar |
growth | float |
safety | int |
Show Desired Output
Desired output:
name | running_total_weight |
UnitedHealth | 10.68 |
Visa | 21.36 |
Johnson & Johnson | 31.03 |
Realty Income | 40.47 |
Texas Instruments | 49.73 |
VICI Properties | 58.71 |
Microsoft | 66.69 |
JPMorgan Chase | 74.66 |
Apple | 81.25 |
Bank of America | 87.71 |
American Express | 93.87 |
Starbucks | 100.00 |
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 dividends order by running_total_weight