CREATE MATERIALIZED VIEW insights.flow_asset_values_mv AS
SELECT
account_group_id,
dim_transaction_date,
asset_id,
asset_type,
CASE WHEN GROUPING(position_type) = 1 THEN 'POSITION' ELSE position_type END AS position_type,
group_currency,
SUM(net_value_group) AS net_value_group,
SUM(net_value_system) AS net_value_system
FROM insights.flow_values_mv
GROUP BY
GROUPING SETS (
(
account_group_id,
dim_transaction_date,
asset_id,
asset_type,
position_type,
group_currency
),
(account_group_id, dim_transaction_date, asset_id, asset_type, group_currency)
)
HAVING
GROUPING(position_type) = 1 OR NOT position_type IS NULL