Let's say I have a continuous aggregate view that tracks the warehouse inventory change daily. The example below is not real, but I tried to simplify it for the purpose of the question.
CREATE MATERIALIZED VIEW inventory_daily
WITH (timescaledb.continuous) AS
SELECT item,
time_bucket(INTERVAL '1 day', 'time') AS bucket,
SUM(item_delta) as daily_change
FROM conditions
GROUP BY item, bucket;
This gives you intraday inventory changes nicely. But what would be the best, or the most CPU efficient way to the get cumulative sum of all inventory changes over the whole lifespan of items? If you sum all changes together, you should have the count how many items there are left in the inventory, for double accounting, for each day.
Can the cumulative sum done in continous aggregated view or is there a better way to do a breakdown of inventory totals, assuming you have just the change as an input? What I hope to accomplish is:
-- Don't know how to do CUMULATIVE SUM
CREATE MATERIALIZED VIEW inventory_daily
WITH (timescaledb.continuous) AS
SELECT item,
time_bucket(INTERVAL '1 day', time) AS bucket,
SUM(item_delta) as daily_change,
CUMULATIVE_SUM(item_total) as total_at_the_end_day,
FROM conditions
GROUP BY item, bucket;
If this cannot be done in an aggregated view (as it looks like based on the comment) what would be the next best option? Manually calculate values for each day?