-- Dimension Trade View
-- This view consolidates trade information from physical purchase and sale contracts and derivatives
CREATE OR REPLACE VIEW public.vw_bi_dim_trade AS 
    SELECT
        "intPurchaseLineId"     AS "intTradeId",
        
        'Physical'              AS "Trade Category", 
        'Purchase'              AS "Trade Type",  
        "strReference"          AS "Trade Ref",
        "dtmContractDate"       AS "Trade Date",
        "strCounterparty"       AS "Counterparty"        
    FROM public.vw_utility_dim_physical_purchase_contract
    WHERE 1=1
      AND "intPurchaseLineId" > 0
UNION ALL
    SELECT
        "intSaleLineId"         AS "intTradeId",
        'Physical'              AS "Trade Category", 
        'Sale'                  AS "Trade Type",  
        "strReference"          AS "Trade Ref",
        "dtmContractDate"       AS "Trade Date",
        "strCounterparty"       AS "Counterparty"        
    FROM public.vw_utility_dim_physical_sale_contract
    WHERE 1=1
      AND "intSaleLineId" > 0;
    -- UNION ALL
    -- Derivative contracts can be added here in the future

ALTER TABLE public.vw_bi_dim_trade
    OWNER TO postgres;      