diff --git a/vw_bi_dim_trade.sql b/vw_bi_dim_trade.sql new file mode 100644 index 0000000..34e34f2 --- /dev/null +++ b/vw_bi_dim_trade.sql @@ -0,0 +1,30 @@ +-- 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; \ No newline at end of file diff --git a/vw_bi_fct_open_position.sql b/vw_bi_fct_open_position.sql new file mode 100644 index 0000000..bb9838d --- /dev/null +++ b/vw_bi_fct_open_position.sql @@ -0,0 +1,35 @@ +CREATE OR REPLACE VIEW vw_bi_fct_open_position +AS + -- Sign physical contracted quantities + SELECT + FTL."intTradeId", + FTL."Trade Category", + FTL."Delivery Date", + CASE + WHEN FTL."Trade Type" = 'Purchase' THEN 1 + ELSE -1 + END * FTL."Quantity" AS "Quantity" + FROM vw_bi_fct_trade_line AS FTL + UNION ALL + -- Deduct delivered quantities + SELECT + FFE."inTradeId", + FFE."Trade Category", + FFE."Event Date" AS "Delivery Date", + CASE + WHEN FFE."Trade Type" = 'Purchase' THEN -1 + ELSE 1 + END * SUM(FFE."Quantity") AS "Quantity" + + FROM vw_bi_fct_trade_fulfillment_event AS FFE + WHERE FFE."Trade Category" = 'Physical' + GROUP BY + FFE."inTradeId", + FFE."Trade Category", + FFE."Event Date", + FFE."Trade Type"; + +ALTER TABLE public.vw_bi_fct_open_position + OWNER TO postgres; + + diff --git a/vw_bi_fct_trade_fulfillment_event.sql b/vw_bi_fct_trade_fulfillment_event.sql new file mode 100644 index 0000000..d92bb92 --- /dev/null +++ b/vw_bi_fct_trade_fulfillment_event.sql @@ -0,0 +1,26 @@ +CREATE OR REPLACE VIEW vw_bi_fct_trade_fulfillment_event AS + SELECT + SM."intPurchaseLineId" AS "inTradeId", + 'Physical' AS "Trade Category", + 'Delivered' AS "Event Type", + SM."dtmBLDate" AS "Event Date", + SM."dblQuantity" AS "Quantity" + FROM "vw_utility_stock_movements" AS SM + WHERE 1=1 + AND COALESCE( SM."intPurchaseLineId" , 0 ) > 0 + UNION ALL + SELECT + SM."intSaleLineId" AS "inTradeId", + 'Physical' AS "Trade Category", + 'Delivered' AS "Event Type", + SM."dtmBLDate" AS "Event Date", + SM."dblQuantity" AS "Quantity" + FROM "vw_utility_stock_movements" AS SM + WHERE 1=1 + AND COALESCE( SM."intSaleLineId" , 0 ) > 0; + -- UNION ALL + -- Derivative contracts can be added here in the future (Settled, Exercised, Expired, etc.) + +ALTER TABLE public.vw_bi_fct_trade_fulfillment_event + OWNER TO postgres; + \ No newline at end of file diff --git a/vw_bi_fct_trade_line.sql b/vw_bi_fct_trade_line.sql new file mode 100644 index 0000000..c7f94f4 --- /dev/null +++ b/vw_bi_fct_trade_line.sql @@ -0,0 +1,38 @@ +-- 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; + + + + + diff --git a/vw_utility_purchase_physical_contract.sql b/vw_utility_purchase_physical_contract.sql index 295ef46..cc348b8 100644 --- a/vw_utility_purchase_physical_contract.sql +++ b/vw_utility_purchase_physical_contract.sql @@ -11,11 +11,11 @@ CREATE OR REPLACE VIEW public.vw_utility_purchase_physical_contract AS COALESCE(vult.dblintransitquantity, 0::double precision) AS "dblInTransitQuantity", COALESCE(dropship.dbldropshippedquantity, 0::double precision) AS "dblDropshippedQuantity", COALESCE(received.dblreceivedquantity, 0::double precision) AS "dblReceivedQuantity", - pl.linked_price AS "dblPrice", + COALESCE(pl.linked_price, 0) AS "dblPrice", pu2.name AS "strPriceUnit", 'n/a'::text AS "strPriceCurrency", pl.unit_price AS "dblUnitPrice", - pl.quantity_theorical AS "dblTheoriticalQuantity", + COALESCE(pl.quantity_theorical, 0::numeric) AS "dblTheoriticalQuantity", round((pl.quantity * pl.unit_price::double precision)::numeric, get_rounding_position(mcr.rounding)) AS "dblLineAmount", cur.name AS "strCurrency", 0::numeric AS "dblLineBaseAmount", @@ -32,7 +32,7 @@ CREATE OR REPLACE VIEW public.vw_utility_purchase_physical_contract AS 0::numeric AS "dblInvoicedAmount", ''::text AS "strInvoiceCurrency", COALESCE(phys.dblphysicalqty, 0::double precision) AS "dblPhysicalQty", - pl.quantity_theorical::double precision - COALESCE(phys.dblphysicalqty, 0::double precision) AS "dblOpenQuantity", + COALESCE(pl.quantity_theorical::double precision, 0::double precision) - COALESCE(phys.dblphysicalqty, 0::double precision) AS "dblOpenQuantity", COALESCE(inst.dblinstructedqty, 0::double precision) AS "dblInInstructedQuantity", COALESCE(bl.estimated_date, pl.to_del) AS "dtmEstimatedBLDate" FROM purchase_line pl diff --git a/vw_utility_sale_physical_contract.sql b/vw_utility_sale_physical_contract.sql index 7ba3214..995c93a 100644 --- a/vw_utility_sale_physical_contract.sql +++ b/vw_utility_sale_physical_contract.sql @@ -9,11 +9,11 @@ CREATE OR REPLACE VIEW public.vw_utility_sale_physical_contract AS sl.quantity AS "dblQuantity", pu1.name AS "strSaleUom", 0 AS "dblDeliveredQuantity", - sl.linked_price AS "dblPrice", + COALESCE(sl.linked_price, 0) AS "dblPrice", pu2.name AS "strPriceUnit", 'n/a'::text AS "strPriceCurrency", sl.unit_price AS "dblUnitPrice", - sl.quantity_theorical AS "dblTheoreticalQuantity", + COALESCE(sl.quantity_theorical,0) AS "dblTheoreticalQuantity", round((sl.quantity * sl.unit_price::double precision)::numeric, get_rounding_position(2::numeric)) AS "dblLineAmount", 'n/a'::text AS "strCurrency", 0::numeric AS "dblLineBaseAmount",