no message

This commit is contained in:
AzureAD\SylvainDUVERNAY
2026-02-02 15:52:52 +01:00
parent 7f99b78c8a
commit 0fa522ece6
6 changed files with 134 additions and 5 deletions

30
vw_bi_dim_trade.sql Normal file
View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

38
vw_bi_fct_trade_line.sql Normal file
View File

@@ -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;

View File

@@ -11,11 +11,11 @@ CREATE OR REPLACE VIEW public.vw_utility_purchase_physical_contract AS
COALESCE(vult.dblintransitquantity, 0::double precision) AS "dblInTransitQuantity", COALESCE(vult.dblintransitquantity, 0::double precision) AS "dblInTransitQuantity",
COALESCE(dropship.dbldropshippedquantity, 0::double precision) AS "dblDropshippedQuantity", COALESCE(dropship.dbldropshippedquantity, 0::double precision) AS "dblDropshippedQuantity",
COALESCE(received.dblreceivedquantity, 0::double precision) AS "dblReceivedQuantity", COALESCE(received.dblreceivedquantity, 0::double precision) AS "dblReceivedQuantity",
pl.linked_price AS "dblPrice", COALESCE(pl.linked_price, 0) AS "dblPrice",
pu2.name AS "strPriceUnit", pu2.name AS "strPriceUnit",
'n/a'::text AS "strPriceCurrency", 'n/a'::text AS "strPriceCurrency",
pl.unit_price AS "dblUnitPrice", 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", round((pl.quantity * pl.unit_price::double precision)::numeric, get_rounding_position(mcr.rounding)) AS "dblLineAmount",
cur.name AS "strCurrency", cur.name AS "strCurrency",
0::numeric AS "dblLineBaseAmount", 0::numeric AS "dblLineBaseAmount",
@@ -32,7 +32,7 @@ CREATE OR REPLACE VIEW public.vw_utility_purchase_physical_contract AS
0::numeric AS "dblInvoicedAmount", 0::numeric AS "dblInvoicedAmount",
''::text AS "strInvoiceCurrency", ''::text AS "strInvoiceCurrency",
COALESCE(phys.dblphysicalqty, 0::double precision) AS "dblPhysicalQty", 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(inst.dblinstructedqty, 0::double precision) AS "dblInInstructedQuantity",
COALESCE(bl.estimated_date, pl.to_del) AS "dtmEstimatedBLDate" COALESCE(bl.estimated_date, pl.to_del) AS "dtmEstimatedBLDate"
FROM purchase_line pl FROM purchase_line pl

View File

@@ -9,11 +9,11 @@ CREATE OR REPLACE VIEW public.vw_utility_sale_physical_contract AS
sl.quantity AS "dblQuantity", sl.quantity AS "dblQuantity",
pu1.name AS "strSaleUom", pu1.name AS "strSaleUom",
0 AS "dblDeliveredQuantity", 0 AS "dblDeliveredQuantity",
sl.linked_price AS "dblPrice", COALESCE(sl.linked_price, 0) AS "dblPrice",
pu2.name AS "strPriceUnit", pu2.name AS "strPriceUnit",
'n/a'::text AS "strPriceCurrency", 'n/a'::text AS "strPriceCurrency",
sl.unit_price AS "dblUnitPrice", 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", round((sl.quantity * sl.unit_price::double precision)::numeric, get_rounding_position(2::numeric)) AS "dblLineAmount",
'n/a'::text AS "strCurrency", 'n/a'::text AS "strCurrency",
0::numeric AS "dblLineBaseAmount", 0::numeric AS "dblLineBaseAmount",