155 lines
8.8 KiB
SQL
155 lines
8.8 KiB
SQL
CREATE OR REPLACE VIEW public.vw_bi_itsa_physical_open_costs AS
|
|
WITH valuation_info AS (
|
|
SELECT pvl."intContractId",
|
|
pvl."intContractLineId",
|
|
pvl."strState",
|
|
avg(pvl."dblPrice") AS "avgPrice",
|
|
(avg(pvl."dblMtmPrice"))::numeric(18,2) AS "avgMtmPrice"
|
|
FROM vw_utility_physical_valuation_line pvl
|
|
GROUP BY pvl."intContractId", pvl."intContractLineId", pvl."strState"
|
|
), contract_open_quantity AS (
|
|
SELECT 'Purchase'::text AS "TradeType",
|
|
pc."intPurchaseLineId" AS "TradeLineId",
|
|
pc."dblTheoriticalQuantity" AS "TheoriticalQuantity",
|
|
pc."dblInInstructedQuantity" AS "InstructedQuantity",
|
|
CASE
|
|
WHEN (pc."ysnFinished" = true) THEN (0)::numeric(18,3)
|
|
WHEN ((pc."dblTheoriticalQuantity")::double precision > pc."dblInInstructedQuantity") THEN (((pc."dblTheoriticalQuantity")::double precision - pc."dblInInstructedQuantity"))::numeric(18,3)
|
|
ELSE (0)::numeric(18,3)
|
|
END AS "OpenQuantity",
|
|
CASE
|
|
WHEN (pc."ysnFinished" = true) THEN (0)::numeric(18,3)
|
|
WHEN ((pc."dblTheoriticalQuantity")::double precision > pc."dblInInstructedQuantity") THEN (((pc."dblTheoriticalQuantity")::double precision - pc."dblInInstructedQuantity"))::numeric(18,3)
|
|
ELSE (0)::numeric(18,3)
|
|
END AS "OpenQuantitySigned",
|
|
COALESCE(vi."avgMtmPrice", (0)::numeric(18,2)) AS "AvgMtmPrice",
|
|
pc."dblTheoriticalQuantity" AS "TheoriticalQuantitySigned"
|
|
FROM (vw_utility_purchase_physical_contract pc
|
|
LEFT JOIN valuation_info vi ON ((pc."intPurchaseLineId" = vi."intContractLineId")))
|
|
WHERE (1 = 1)
|
|
UNION ALL
|
|
SELECT 'Sale'::text AS "TradeType",
|
|
pc."intSaleLineId" AS "TradeLineId",
|
|
pc."dblTheoreticalQuantity" AS "TheoriticalQuantity",
|
|
pc."dblInstructedQuantity" AS "InstructedQuantity",
|
|
CASE
|
|
WHEN (pc."ysnFinished" = true) THEN (0)::numeric(18,3)
|
|
WHEN ((pc."dblTheoreticalQuantity")::double precision > pc."dblInstructedQuantity") THEN (((pc."dblTheoreticalQuantity")::double precision - pc."dblInstructedQuantity"))::numeric(18,3)
|
|
ELSE (0)::numeric(18,3)
|
|
END AS "OpenQuantity",
|
|
(
|
|
CASE
|
|
WHEN (pc."ysnFinished" = true) THEN (0)::numeric(18,3)
|
|
WHEN ((pc."dblTheoreticalQuantity")::double precision > pc."dblInstructedQuantity") THEN (((pc."dblTheoreticalQuantity")::double precision - pc."dblInstructedQuantity"))::numeric(18,3)
|
|
ELSE (0)::numeric(18,3)
|
|
END * ('-1'::integer)::numeric) AS "OpenQuantitySigned",
|
|
COALESCE(vi."avgMtmPrice", (0)::numeric(18,2)) AS "AvgMtmPrice",
|
|
(pc."dblTheoreticalQuantity" * ('-1'::integer)::numeric) AS "TheoriticalQuantitySigned"
|
|
FROM (vw_utility_sale_physical_contract pc
|
|
LEFT JOIN valuation_info vi ON ((pc."intSaleLineId" = vi."intContractLineId")))
|
|
WHERE (1 = 1)
|
|
)
|
|
SELECT 1 AS "Group Order",
|
|
'Price'::text AS "Group Type",
|
|
ctr."intPurchaseLineId" AS "intTradeLineId",
|
|
'Fixed Price'::character varying AS "Line Description",
|
|
pur."dblUnitPrice" AS "Price",
|
|
opq."OpenQuantitySigned" AS "Open Quantity Signed",
|
|
((opq."OpenQuantitySigned" * pur."dblUnitPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
|
|
1 AS "intPriceGroupId",
|
|
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
|
|
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
|
|
'Price'::text AS "Line Group"
|
|
FROM ((vw_utility_dim_physical_purchase_contract ctr
|
|
JOIN vw_utility_purchase_physical_contract pur ON ((ctr."intPurchaseLineId" = pur."intPurchaseLineId")))
|
|
JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
|
|
WHERE (((ctr."strPricingType")::text = 'priced'::text) AND (opq."OpenQuantity" <> (0)::numeric))
|
|
UNION ALL
|
|
SELECT 2 AS "Group Order",
|
|
'MTM'::text AS "Group Type",
|
|
ctr."intPurchaseLineId" AS "intTradeLineId",
|
|
mtm."strComponentCurve" AS "Line Description",
|
|
opq."AvgMtmPrice" AS "Price",
|
|
opq."OpenQuantitySigned" AS "Open Quantity Signed",
|
|
(opq."OpenQuantitySigned" * opq."AvgMtmPrice") AS "Open Cost Amount",
|
|
3 AS "intPriceGroupId",
|
|
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
|
|
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
|
|
'MTM'::text AS "Line Group"
|
|
FROM ((vw_utility_physical_mtm_definition mtm
|
|
JOIN vw_utility_dim_physical_purchase_contract ctr ON ((mtm."intContractLineId" = ctr."intPurchaseLineId")))
|
|
JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
|
|
WHERE ((mtm."strContractType" = 'Purchase'::text) AND (opq."OpenQuantity" <> (0)::numeric))
|
|
UNION ALL
|
|
SELECT 3 AS "Group Order",
|
|
'Fee'::text AS "Group Type",
|
|
fees."intContractLineId" AS "intTradeLineId",
|
|
fees."strProduct" AS "Line Description",
|
|
fees."dblPrice" AS "Price",
|
|
opq."OpenQuantitySigned" AS "Open Quantity Signed",
|
|
((((
|
|
CASE
|
|
WHEN ((fees."strPayOrRec")::text = 'pay'::text) THEN 1
|
|
ELSE '-1'::integer
|
|
END)::numeric * opq."OpenQuantity") * fees."dblPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
|
|
2 AS "intPriceGroupId",
|
|
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
|
|
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
|
|
fees."strProductGroup" AS "Line Group"
|
|
FROM ((vw_utility_contract_fees fees
|
|
JOIN vw_utility_dim_physical_purchase_contract ctr ON ((fees."intContractLineId" = ctr."intPurchaseLineId")))
|
|
JOIN contract_open_quantity opq ON (((ctr."intPurchaseLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Purchase'::text))))
|
|
WHERE ((fees."strContractType" = 'Purchase'::text) AND (opq."OpenQuantity" <> (0)::numeric))
|
|
UNION ALL
|
|
SELECT 1 AS "Group Order",
|
|
'Price'::text AS "Group Type",
|
|
ctr."intSaleLineId" AS "intTradeLineId",
|
|
'Fixed Price'::character varying AS "Line Description",
|
|
sale."dblUnitPrice" AS "Price",
|
|
opq."OpenQuantitySigned" AS "Open Quantity Signed",
|
|
((opq."OpenQuantitySigned" * sale."dblUnitPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
|
|
1 AS "intPriceGroupId",
|
|
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
|
|
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
|
|
'Price'::text AS "Line Group"
|
|
FROM ((vw_utility_dim_physical_sale_contract ctr
|
|
JOIN vw_utility_sale_physical_contract sale ON ((ctr."intSaleLineId" = sale."intSaleLineId")))
|
|
JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))
|
|
WHERE (((ctr."strPricingType")::text = 'priced'::text) AND (opq."OpenQuantity" <> (0)::numeric))
|
|
UNION ALL
|
|
SELECT 2 AS "Group Order",
|
|
'MTM'::text AS "Group Type",
|
|
ctr."intSaleLineId" AS "intTradeLineId",
|
|
mtm."strComponentCurve" AS "Line Description",
|
|
opq."AvgMtmPrice" AS "Price",
|
|
opq."OpenQuantitySigned" AS "Open Quantity Signed",
|
|
(opq."OpenQuantitySigned" * opq."AvgMtmPrice") AS "Open Cost Amount",
|
|
3 AS "intPriceGroupId",
|
|
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
|
|
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
|
|
'MTM'::text AS "Line Group"
|
|
FROM ((vw_utility_physical_mtm_definition mtm
|
|
JOIN vw_utility_dim_physical_sale_contract ctr ON ((mtm."intContractLineId" = ctr."intSaleLineId")))
|
|
JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))
|
|
WHERE ((mtm."strContractType" = 'Sale'::text) AND (opq."OpenQuantity" <> (0)::numeric))
|
|
UNION ALL
|
|
SELECT 3 AS "Group Order",
|
|
'Fee'::text AS "Group Type",
|
|
fees."intContractLineId" AS "intTradeLineId",
|
|
fees."strProduct" AS "Line Description",
|
|
fees."dblPrice" AS "Price",
|
|
opq."OpenQuantitySigned" AS "Open Quantity Signed",
|
|
((((
|
|
CASE
|
|
WHEN ((fees."strPayOrRec")::text = 'pay'::text) THEN 1
|
|
ELSE '-1'::integer
|
|
END)::numeric * opq."OpenQuantity") * fees."dblPrice") * ('-1'::integer)::numeric) AS "Open Cost Amount",
|
|
2 AS "intPriceGroupId",
|
|
ctr."dtmEstimatedBLDate" AS "Estimated BL Date",
|
|
opq."TheoriticalQuantitySigned" AS "Contract Quantity Signed",
|
|
fees."strProductGroup" AS "Line Group"
|
|
FROM ((vw_utility_contract_fees fees
|
|
JOIN vw_utility_dim_physical_sale_contract ctr ON ((fees."intContractLineId" = ctr."intSaleLineId")))
|
|
JOIN contract_open_quantity opq ON (((ctr."intSaleLineId" = opq."TradeLineId") AND (opq."TradeType" = 'Sale'::text))))
|
|
WHERE ((fees."strContractType" = 'Sale'::text) AND (opq."OpenQuantity" <> (0)::numeric));;
|