CREATE MATERIALIZED VIEW adib_rm.pnl_snapshot_mv AS
SELECT
account_group_id,
dim_balance_date,
position_type,
currency_code,
unrealized_gain_loss,
total_average_cost,
unrealized_gain_loss_system_currency,
total_average_cost_system_currency
FROM (
SELECT
account_group_id,
dim_balance_date,
position_type,
currency_code,
unrealized_gain_loss,
total_average_cost,
unrealized_gain_loss_system_currency,
total_average_cost_system_currency,
ROW_NUMBER() OVER (
PARTITION BY account_group_id, position_type, currency_code
ORDER BY dim_balance_date DESC
) AS rn
FROM insights.pnl_summary_mv AS pnl_summary_mv_next
)
WHERE
rn = 1