CREATE MATERIALIZED VIEW insights.settled_cost_basis_dedup_mv AS
SELECT
account_id,
asset_id,
type,
currency_code,
dim_value_date,
average_cost_per_unit,
average_cost_per_unit_system_currency,
total_cost_system_currency,
cost_fx_provenance,
purchased_quantity,
m_is_stub
FROM (
SELECT
account_id,
asset_id,
type,
currency_code,
dim_value_date,
average_cost_per_unit,
average_cost_per_unit_system_currency,
total_cost_system_currency,
cost_fx_provenance,
purchased_quantity,
m_is_stub,
ROW_NUMBER() OVER (
PARTITION BY account_id, asset_id, currency_code, dim_value_date
ORDER BY (
m_is_stub IS TRUE
), type
) AS rn
FROM olap.holding_values_raw_ft
WHERE
disabled_at IS NULL
) AS d
WHERE
rn = 1