Running Totals
Question:
The stocks must be ordered by name and will enter our portfolio one by one. We would like to know what the running total dividend is.
Return: name, running total dividend
Order by: name
Show Table
Dividends:
stock | varchar |
sector | varchar |
dividend | float |
frequency | varchar |
growth | float |
safety | int |
Show Desired Output
Desired output:
name | running_total_dividend |
American Express | 1.19 |
Apple | 1.74 |
Bank of America | 4.28 |
JPMorgan Chase | 6.68 |
Johnson & Johnson | 10.00 |
Microsoft | 10.73 |
Realty Income | 16.29 |
Starbucks | 19.41 |
Texas Instruments | 22.32 |
UnitedHealth | 23.85 |
VICI Properties | 29.61 |
Visa | 30.38 |
Query Window:
Correct output but can you use 'over'?
×
Good work!
Over allows us to break down our aggregate functions, it is often used for running totals next question
Show Answer
Answer:
select name, sum(dividend)
over (order by name) as running_total_dividend
from dividends order by name