CREATE OR REPLACE VIEW public.vw_bi_dim_conformed_reference AS
 SELECT pc."strTradeId" AS "strConformedReferenceId",
    pc."Contract Ref" AS "Conformed Trade Ref",
    'Purchase'::text AS "Trade Type",
    pc."Price Fixing Status",
    pc."Bassin",
    pc."Book",
    pc."Supplier Country" AS "Counterparty Country",
    pc."MtM Strategy",
    pc."MtM Strategy Aternative Name"
   FROM vw_bi_dim_purchase_physical_contract pc
  WHERE ((1 = 1) AND (pc."intPurchaseLineId" > 0))
UNION ALL
 SELECT sc."strTradeLineId" AS "strConformedReferenceId",
    sc."Contract Ref" AS "Conformed Trade Ref",
    'Sale'::text AS "Trade Type",
    sc."Price Fixing Status",
    sc."Bassin",
    sc."Book",
    sc."Customer Country" AS "Counterparty Country",
    sc."MtM Strategy",
    sc."MtM Strategy Aternative Name"
   FROM vw_bi_dim_sale_physical_contract sc
  WHERE ((1 = 1) AND (sc."intSaleLineId" > 0))
UNION ALL
 SELECT DISTINCT ((s."intPurchaseLineId" || '_'::text) || s."intSaleLineId") AS "strConformedReferenceId",
        CASE
            WHEN ((pc."Contract Ref")::text <> (sc."Contract Ref")::text) THEN ((((pc."Contract Ref")::text || ' / '::text) || (sc."Contract Ref")::text))::character varying
            ELSE pc."Contract Ref"
        END AS "Conformed Trade Ref",
    'Shipped'::text AS "Trade Type",
        CASE
            WHEN ((pc."Price Fixing Status" = sc."Price Fixing Status") AND (pc."Price Fixing Status" = 'Fixed'::text)) THEN 'Fixed'::text
            WHEN ((pc."Price Fixing Status" = sc."Price Fixing Status") AND (pc."Price Fixing Status" = 'Unfixed'::text)) THEN 'Unfixed'::text
            ELSE 'Partially Fixed'::text
        END AS "Price Fixing Status",
    pc."Bassin",
    pc."Book",
    sc."Customer Country" AS "Counterparty Country",
    sc."MtM Strategy",
    sc."MtM Strategy Aternative Name"
   FROM ((vw_bi_itsa_fct_shipments s
     JOIN vw_bi_dim_purchase_physical_contract pc ON ((s."intPurchaseLineId" = pc."intPurchaseLineId")))
     JOIN vw_bi_dim_sale_physical_contract sc ON ((s."intSaleLineId" = sc."intSaleLineId")))
  WHERE (1 = 1)
UNION ALL
 SELECT '0_0'::text AS "strConformedReferenceId",
    'N/A'::text AS "Conformed Trade Ref",
    'Monthly Adjustment'::text AS "Trade Type",
    'Fixed'::text AS "Price Fixing Status",
    'N/A'::text AS "Bassin",
    'N/A'::text AS "Book",
    'N/A'::text AS "Counterparty Country",
    'N/A'::text AS "MtM Strategy",
    'N/A'::text AS "MtM Strategy Aternative Name";;
