-- Fact Trade Line View
-- This view consolidates trade line data from both purchase and sale physical contracts and derivatives
CREATE OR REPLACE VIEW vw_bi_fct_trade_line AS
    SELECT 
        pc."intPurchaseLineId"          AS "intTradeId",
        'Physical'                      AS "Trade Category",
        'Purchase'                      AS "Trade Type", 
        pc."dtmEstimatedBLDate"         AS "Delivery Date",
        pc."dblTheoriticalQuantity"     AS "Contracted Quantity",
        pc."dblQuantity"                AS "Quantity",
        pc."dblPrice"                   AS "Price",
        pc."dblUnitPrice"               AS "Unit Price",
        'USD'                           AS "Price Currency",
        pc."dblLineAmount"              AS "Line Amount"
    FROM vw_utility_purchase_physical_contract pc
    UNION ALL    
    SELECT 
        pc."intSaleLineId"              AS "intTradeId",
        'Physical'                      AS "Trade Category",
        'Sale'                          AS "Trade Type", 
        pc."dtmEstimatedBLDate"         AS "Delivery Date",
        pc."dblTheoreticalQuantity"     AS "Contracted Quantity",
        pc."dblQuantity"                AS "Quantity",
        pc."dblPrice"                   AS "Price",
        pc."dblUnitPrice"               AS "Unit Price",
        'USD'                           AS "Price Currency",
        pc."dblLineAmount"              AS "Line Amount"
    FROM vw_utility_sale_physical_contract pc;
    -- UNION ALL
    -- Derivative contracts can be added here in the future

ALTER TABLE public.vw_bi_fct_trade_line
    OWNER TO postgres;        
    

    


