CREATE MATERIALIZED VIEW insights.flow_summary_mv AS
SELECT
account_group_id,
dim_transaction_date,
position_type,
currency_code,
adjusted_net_flow,
adjusted_net_flow_system_currency,
SUM(adjusted_net_flow) OVER w AS cumulative_adjusted_netflow,
SUM(adjusted_net_flow_system_currency) OVER w AS cumulative_adjusted_netflow_system_currency
FROM (
SELECT
account_group_id,
dim_transaction_date,
position_type,
group_currency AS currency_code,
SUM(net_value_group) AS adjusted_net_flow,
SUM(net_value_system) AS adjusted_net_flow_system_currency
FROM insights.flow_asset_values_mv
GROUP BY
account_group_id,
dim_transaction_date,
position_type,
group_currency
) AS grouped
WINDOW w AS (
PARTITION BY account_group_id, currency_code, position_type
ORDER BY dim_transaction_date
)