diff --git a/Database Backups/SQL Views/vw_bi_dim_conformed_reference.sql b/Database Backups/SQL Views/vw_bi_dim_conformed_reference.sql new file mode 100644 index 0000000..3e4c17a --- /dev/null +++ b/Database Backups/SQL Views/vw_bi_dim_conformed_reference.sql @@ -0,0 +1,39 @@ +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" + 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" + 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" + 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);; diff --git a/Database Backups/SQL Views/vw_bi_dim_purchase_physical_contract.sql b/Database Backups/SQL Views/vw_bi_dim_purchase_physical_contract.sql index 97f3fdf..cdae327 100644 --- a/Database Backups/SQL Views/vw_bi_dim_purchase_physical_contract.sql +++ b/Database Backups/SQL Views/vw_bi_dim_purchase_physical_contract.sql @@ -1,46 +1,61 @@ CREATE OR REPLACE VIEW public.vw_bi_dim_purchase_physical_contract AS - SELECT "intPurchaseLineId", - "strContractNb" AS "Contract Nb", - "strCounterparty" AS "Supplier", - "strReference" AS "Contract Ref", - "dtmContractDate" AS "Contract Date", + WITH pricingstatus AS ( + SELECT pvl_1."intContractLineId", + pvl_1."strState" + FROM vw_utility_physical_valuation_line pvl_1 + GROUP BY pvl_1."intContractLineId", pvl_1."strState" + ) + SELECT dpc."intPurchaseLineId", + dpc."strContractNb" AS "Contract Nb", + dpc."strCounterparty" AS "Supplier", + dpc."strReference" AS "Contract Ref", + dpc."dtmContractDate" AS "Contract Date", CASE - WHEN (("strContractStatus")::text = 'Draft'::text) THEN ((("strContractStatus")::text || ' 🧾'::text))::character varying - WHEN (("strContractStatus")::text = 'Confirmed'::text) THEN ((("strContractStatus")::text || ' ✅'::text))::character varying - WHEN (("strContractStatus")::text = 'Validated'::text) THEN ((("strContractStatus")::text || ' ✔️'::text))::character varying - WHEN (("strContractStatus")::text = 'Processing'::text) THEN ((("strContractStatus")::text || ' ⏳'::text))::character varying - ELSE "strContractStatus" + WHEN ((dpc."strContractStatus")::text = 'Draft'::text) THEN (((dpc."strContractStatus")::text || ' 🧾'::text))::character varying + WHEN ((dpc."strContractStatus")::text = 'Confirmed'::text) THEN (((dpc."strContractStatus")::text || ' ✅'::text))::character varying + WHEN ((dpc."strContractStatus")::text = 'Validated'::text) THEN (((dpc."strContractStatus")::text || ' ✔️'::text))::character varying + WHEN ((dpc."strContractStatus")::text = 'Processing'::text) THEN (((dpc."strContractStatus")::text || ' ⏳'::text))::character varying + ELSE dpc."strContractStatus" END AS "Contract Status", - "strPaymentTerm" AS "Payment Terms", - "strCurrency" AS "Currency", - "strWeightBasis" AS "Weight Basis", - "strBroker" AS "Broker", - "strCertif" AS "Certification", - "strAssociation" AS "Association", - "strCrop" AS "Crop", - "dblTolMinPct" AS "Tolerance Min %", - "dblTolMaxPct" AS "Tolerance Max %", - "strIncoterm" AS "Inco Terms", - "strLoadingPlace" AS "Loading Place", - "strDestinationPlace" AS "Destination Place", - "strProduct" AS "Product", - "strDeliveryPeriod" AS "Delivery Period", - "strRegion" AS "Bassin", - "strBookName" AS "Book", - "strStrategyName" AS "Strategy", - "strQuantityUom" AS "Quantity UOM", - "dtmDeliveryDateFrom" AS "Delivery Date From", - "dtmDeliveryDateTo" AS "Delivery Date To", + dpc."strPaymentTerm" AS "Payment Terms", + dpc."strCurrency" AS "Currency", + dpc."strWeightBasis" AS "Weight Basis", + dpc."strBroker" AS "Broker", + dpc."strCertif" AS "Certification", + dpc."strAssociation" AS "Association", + dpc."strCrop" AS "Crop", + dpc."dblTolMinPct" AS "Tolerance Min %", + dpc."dblTolMaxPct" AS "Tolerance Max %", + dpc."strIncoterm" AS "Inco Terms", + dpc."strLoadingPlace" AS "Loading Place", + dpc."strDestinationPlace" AS "Destination Place", + dpc."strProduct" AS "Product", + dpc."strDeliveryPeriod" AS "Delivery Period", + dpc."strRegion" AS "Bassin", + dpc."strBookName" AS "Book", + dpc."strStrategyName" AS "Strategy", + dpc."strQuantityUom" AS "Quantity UOM", + dpc."dtmDeliveryDateFrom" AS "Delivery Date From", + dpc."dtmDeliveryDateTo" AS "Delivery Date To", CASE - WHEN ("dtmDeliveryDateTo" < CURRENT_DATE) THEN 'Overdue'::text - WHEN (("dtmDeliveryDateFrom" <= CURRENT_DATE) AND ("dtmDeliveryDateTo" >= CURRENT_DATE)) THEN 'Current'::text - WHEN ("dtmDeliveryDateFrom" > CURRENT_DATE) THEN 'Future'::text + WHEN (dpc."dtmDeliveryDateTo" < CURRENT_DATE) THEN 'Overdue'::text + WHEN ((dpc."dtmDeliveryDateFrom" <= CURRENT_DATE) AND (dpc."dtmDeliveryDateTo" >= CURRENT_DATE)) THEN 'Current'::text + WHEN (dpc."dtmDeliveryDateFrom" > CURRENT_DATE) THEN 'Future'::text ELSE 'Unknown'::text END AS "Delivery Status", CASE - WHEN (("strBookName")::text ~~ '%Projection%'::text) THEN 'Yes'::text + WHEN ((dpc."strBookName")::text ~~ '%Projection%'::text) THEN 'Yes'::text ELSE 'No'::text END AS "Is Projection", - "strIncotermCode" AS "Incoterm Code", - "dtmEstimatedBLDate" AS "Estimated BL Date" - FROM vw_utility_dim_physical_purchase_contract dpc;; + dpc."strIncotermCode" AS "Incoterm Code", + dpc."dtmEstimatedBLDate" AS "Estimated BL Date", + dpc."strPricingType" AS "Pricing Type", + COALESCE(mtm."strStrategy", 'Undefined'::character varying) AS "MtM Strategy", + COALESCE(mtm."strScenario", 'Undefined'::character varying) AS "MtM Scenario", + COALESCE(mtm."strComponentCurve", 'Undefined'::character varying) AS "MtM Component Curve", + dpc."strCounterpartyCountry" AS "Supplier Country", + COALESCE(pvl."strState", ('Undefined'::character varying)::text) AS "Price Fixing Status", + (dpc."intPurchaseLineId" || '_0'::text) AS "strTradeId" + FROM ((vw_utility_dim_physical_purchase_contract dpc + LEFT JOIN vw_utility_physical_mtm_definition mtm ON (((dpc."intPurchaseLineId" = mtm."intContractLineId") AND (mtm."strContractType" = 'Purchase'::text) AND ((mtm."strComponentFixType")::text = 'Market price'::text)))) + LEFT JOIN pricingstatus pvl ON ((dpc."intPurchaseLineId" = pvl."intContractLineId")));; diff --git a/Database Backups/SQL Views/vw_bi_dim_sale_physical_contract.sql b/Database Backups/SQL Views/vw_bi_dim_sale_physical_contract.sql index 74c32b6..72ede5a 100644 --- a/Database Backups/SQL Views/vw_bi_dim_sale_physical_contract.sql +++ b/Database Backups/SQL Views/vw_bi_dim_sale_physical_contract.sql @@ -1,46 +1,61 @@ CREATE OR REPLACE VIEW public.vw_bi_dim_sale_physical_contract AS - SELECT "intSaleLineId", - "strContractNb" AS "Contract Nb", - "strCounterparty" AS "Customer", - "strReference" AS "Contract Ref", - "dtmContractDate" AS "Contract Date", + WITH pricingstatus AS ( + SELECT pvl_1."intContractLineId", + pvl_1."strState" + FROM vw_utility_physical_valuation_line pvl_1 + GROUP BY pvl_1."intContractLineId", pvl_1."strState" + ) + SELECT dpc."intSaleLineId", + dpc."strContractNb" AS "Contract Nb", + dpc."strCounterparty" AS "Customer", + dpc."strReference" AS "Contract Ref", + dpc."dtmContractDate" AS "Contract Date", CASE - WHEN (("strContractStatus")::text = 'Draft'::text) THEN ((("strContractStatus")::text || ' 🧾'::text))::character varying - WHEN (("strContractStatus")::text = 'Confirmed'::text) THEN ((("strContractStatus")::text || ' ✅'::text))::character varying - WHEN (("strContractStatus")::text = 'Validated'::text) THEN ((("strContractStatus")::text || ' ✔️'::text))::character varying - WHEN (("strContractStatus")::text = 'Processing'::text) THEN ((("strContractStatus")::text || ' ⏳'::text))::character varying - ELSE "strContractStatus" + WHEN ((dpc."strContractStatus")::text = 'Draft'::text) THEN (((dpc."strContractStatus")::text || ' 🧾'::text))::character varying + WHEN ((dpc."strContractStatus")::text = 'Confirmed'::text) THEN (((dpc."strContractStatus")::text || ' ✅'::text))::character varying + WHEN ((dpc."strContractStatus")::text = 'Validated'::text) THEN (((dpc."strContractStatus")::text || ' ✔️'::text))::character varying + WHEN ((dpc."strContractStatus")::text = 'Processing'::text) THEN (((dpc."strContractStatus")::text || ' ⏳'::text))::character varying + ELSE dpc."strContractStatus" END AS "Contract Status", - "strPaymentTerm" AS "Payment Terms", - "strCurrency" AS "Currency", - "strWeightBasis" AS "Weight Basis", - "strBroker" AS "Broker", - "strCertif" AS "Certification", - "strAssociation" AS "Association", - "strCrop" AS "Crop", - "dblTolMinPct" AS "Tolerance Min %", - "dblTolMaxPct" AS "Tolerance Max %", - "strIncoterm" AS "Inco Terms", - "strLoadingPlace" AS "Loading Place", - "strDestinationPlace" AS "Destination Place", - "strProduct" AS "Product", - "strDeliveryPeriod" AS "Delivery Period", - "strQuantityUom" AS "Quantity UOM", - "dtmDeliveryDateFrom" AS "Delivery Date From", - "dtmDeliveryDateTo" AS "Delivery Date To", + dpc."strPaymentTerm" AS "Payment Terms", + dpc."strCurrency" AS "Currency", + dpc."strWeightBasis" AS "Weight Basis", + dpc."strBroker" AS "Broker", + dpc."strCertif" AS "Certification", + dpc."strAssociation" AS "Association", + dpc."strCrop" AS "Crop", + dpc."dblTolMinPct" AS "Tolerance Min %", + dpc."dblTolMaxPct" AS "Tolerance Max %", + dpc."strIncoterm" AS "Inco Terms", + dpc."strLoadingPlace" AS "Loading Place", + dpc."strDestinationPlace" AS "Destination Place", + dpc."strProduct" AS "Product", + dpc."strDeliveryPeriod" AS "Delivery Period", + dpc."strQuantityUom" AS "Quantity UOM", + dpc."dtmDeliveryDateFrom" AS "Delivery Date From", + dpc."dtmDeliveryDateTo" AS "Delivery Date To", CASE - WHEN ("dtmDeliveryDateTo" < CURRENT_DATE) THEN 'Overdue'::text - WHEN (("dtmDeliveryDateFrom" <= CURRENT_DATE) AND ("dtmDeliveryDateTo" >= CURRENT_DATE)) THEN 'Current'::text - WHEN ("dtmDeliveryDateFrom" > CURRENT_DATE) THEN 'Future'::text + WHEN (dpc."dtmDeliveryDateTo" < CURRENT_DATE) THEN 'Overdue'::text + WHEN ((dpc."dtmDeliveryDateFrom" <= CURRENT_DATE) AND (dpc."dtmDeliveryDateTo" >= CURRENT_DATE)) THEN 'Current'::text + WHEN (dpc."dtmDeliveryDateFrom" > CURRENT_DATE) THEN 'Future'::text ELSE 'Unknown'::text END AS "Delivery Status", - "strRegion" AS "Bassin", - "strBookName" AS "Book", - "strStrategyName" AS "Strategy", + dpc."strRegion" AS "Bassin", + dpc."strBookName" AS "Book", + dpc."strStrategyName" AS "Strategy", CASE - WHEN (("strBookName")::text ~~ '%Projection%'::text) THEN 'Yes'::text + WHEN ((dpc."strBookName")::text ~~ '%Projection%'::text) THEN 'Yes'::text ELSE 'No'::text END AS "Is Projection", - "strIncotermCode" AS "Incoterm Code", - "dtmEstimatedBLDate" AS "Estimated BL Date" - FROM vw_utility_dim_physical_sale_contract dpc;; + dpc."strIncotermCode" AS "Incoterm Code", + dpc."dtmEstimatedBLDate" AS "Estimated BL Date", + dpc."strPricingType" AS "Pricing Type", + COALESCE(mtm."strStrategy", 'Undefined'::character varying) AS "MtM Strategy", + COALESCE(mtm."strScenario", 'Undefined'::character varying) AS "MtM Scenario", + COALESCE(mtm."strComponentCurve", 'Undefined'::character varying) AS "MtM Component Curve", + dpc."strCounterpartyCountry" AS "Customer Country", + COALESCE(pvl."strState", ('Undefined'::character varying)::text) AS "Price Fixing Status", + ('0_'::text || dpc."intSaleLineId") AS "strTradeLineId" + FROM ((vw_utility_dim_physical_sale_contract dpc + LEFT JOIN vw_utility_physical_mtm_definition mtm ON (((dpc."intSaleLineId" = mtm."intContractLineId") AND (mtm."strContractType" = 'Sale'::text) AND ((mtm."strComponentFixType")::text = 'Market price'::text)))) + LEFT JOIN pricingstatus pvl ON ((dpc."intSaleLineId" = pvl."intContractLineId")));; diff --git a/Database Backups/SQL Views/vw_bi_dim_trade.sql b/Database Backups/SQL Views/vw_bi_dim_trade.sql index bae32fd..cb21ffd 100644 --- a/Database Backups/SQL Views/vw_bi_dim_trade.sql +++ b/Database Backups/SQL Views/vw_bi_dim_trade.sql @@ -1,8 +1,10 @@ CREATE OR REPLACE VIEW public.vw_bi_dim_trade AS SELECT pc."intPurchaseLineId" AS "intTradeId", + (pc."intPurchaseLineId" || '_0'::text) AS "strTradeId", 'Physical'::text AS "Trade Category", 'Purchase'::text AS "Trade Type", pc."strReference" AS "Trade Ref", + pc."strContractNb" AS "Trade Number", pc."dtmContractDate" AS "Trade Date", pc."strCounterparty" AS "Counterparty", pc."strRegion" AS "Bassin", @@ -12,9 +14,11 @@ CREATE OR REPLACE VIEW public.vw_bi_dim_trade AS WHERE ((1 = 1) AND (pc."intPurchaseLineId" > 0)) UNION ALL SELECT sc."intSaleLineId" AS "intTradeId", + ('0_'::text || sc."intSaleLineId") AS "strTradeId", 'Physical'::text AS "Trade Category", 'Sale'::text AS "Trade Type", sc."strReference" AS "Trade Ref", + sc."strContractNb" AS "Trade Number", sc."dtmContractDate" AS "Trade Date", sc."strCounterparty" AS "Counterparty", sc."strRegion" AS "Bassin", diff --git a/Database Backups/SQL Views/vw_bi_itsa_dim_pnl_type.sql b/Database Backups/SQL Views/vw_bi_itsa_dim_pnl_type.sql new file mode 100644 index 0000000..28dcdd4 --- /dev/null +++ b/Database Backups/SQL Views/vw_bi_itsa_dim_pnl_type.sql @@ -0,0 +1,9 @@ +CREATE OR REPLACE VIEW public.vw_bi_itsa_dim_pnl_type AS + SELECT 1 AS "intPnlTypeId", + 'Expected'::text AS "PNL Type" +UNION ALL + SELECT 3 AS "intPnlTypeId", + 'Realized'::text AS "PNL Type" +UNION ALL + SELECT 2 AS "intPnlTypeId", + 'Forward'::text AS "PNL Type";; diff --git a/Database Backups/SQL Views/vw_bi_itsa_dim_price_group.sql b/Database Backups/SQL Views/vw_bi_itsa_dim_price_group.sql new file mode 100644 index 0000000..d978e5b --- /dev/null +++ b/Database Backups/SQL Views/vw_bi_itsa_dim_price_group.sql @@ -0,0 +1,12 @@ +CREATE OR REPLACE VIEW public.vw_bi_itsa_dim_price_group AS + SELECT 1 AS "intPriceGroupId", + 'Price'::text AS "Price Group", + 1 AS "Group Order" +UNION ALL + SELECT 2 AS "intPriceGroupId", + 'Fees'::text AS "Price Group", + 2 AS "Group Order" +UNION ALL + SELECT 3 AS "intPriceGroupId", + 'MTM'::text AS "Price Group", + 2 AS "Group Order";; diff --git a/Database Backups/SQL Views/vw_bi_itsa_fct_contract.sql b/Database Backups/SQL Views/vw_bi_itsa_fct_contract.sql index 24d904b..09a3f92 100644 --- a/Database Backups/SQL Views/vw_bi_itsa_fct_contract.sql +++ b/Database Backups/SQL Views/vw_bi_itsa_fct_contract.sql @@ -1,30 +1,49 @@ CREATE OR REPLACE VIEW public.vw_bi_itsa_fct_contract AS + WITH valuation_info AS ( + SELECT pvl."intContractId", + pvl."intContractLineId", + pvl."strState", + avg(pvl."dblPrice") AS "avgPrice", + avg(pvl."dblMtmPrice") AS "avgMtmPrice" + FROM vw_utility_physical_valuation_line pvl + GROUP BY pvl."intContractId", pvl."intContractLineId", pvl."strState" + ) SELECT 'Purchase'::text AS "TradeType", pc."intPurchaseLineId" AS "TradeLineId", pc."dblQuantity" AS "Quantity", - pc."dblTheoriticalQuantity" AS "TheoriticalQuantity", + pc."dblTheoriticalQuantity" AS "Contract Quantity", 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", - pc."dblUnitPrice" AS "Price", - COALESCE(pc."dtmEstimatedBLDate", pc."dtmDeliveryDateFrom") AS "DeliveryDate" - FROM vw_utility_purchase_physical_contract pc + END AS "Open Quantity", + COALESCE(pc."dtmEstimatedBLDate", pc."dtmDeliveryDateFrom") AS "Delivery Date", + COALESCE(vi."strState", 'Unknown'::text) AS "Price Fixing Status", + COALESCE(vi."avgPrice", (0)::numeric) AS "Avg Price", + COALESCE(vi."avgMtmPrice", (0)::numeric) AS "Avg Mtm Price", + pc."dblTheoriticalQuantity" AS "Contract Quantity Signed", + pc."dblInInstructedQuantity" AS "Instructed Quantity Signed" + 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."dblQuantity" AS "Quantity", - pc."dblTheoreticalQuantity" AS "TheoriticalQuantity", + pc."dblTheoreticalQuantity" AS "Contract Quantity", 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", - pc."dblUnitPrice" AS "Price", - COALESCE(pc."dtmEstimatedBLDate", pc."dtmDeliveryDateFrom") AS "DeliveryDate" - FROM vw_utility_sale_physical_contract pc + END AS "Open Quantity", + COALESCE(pc."dtmEstimatedBLDate", pc."dtmDeliveryDateFrom") AS "Delivery Date", + COALESCE(vi."strState", 'Unknown'::text) AS "Price Fixing Status", + COALESCE(vi."avgPrice", (0)::numeric) AS "Avg Price", + COALESCE(vi."avgMtmPrice", (0)::numeric) AS "Avg Mtm Price", + (pc."dblTheoreticalQuantity" * ('-1'::integer)::numeric) AS "Contract Quantity Signed", + (pc."dblInstructedQuantity" * (('-1'::integer)::numeric)::double precision) AS "Instructed Quantity Signed" + FROM (vw_utility_sale_physical_contract pc + LEFT JOIN valuation_info vi ON ((pc."intSaleLineId" = vi."intContractLineId"))) WHERE (1 = 1);; diff --git a/Database Backups/SQL Views/vw_bi_itsa_fct_full_pnl.sql b/Database Backups/SQL Views/vw_bi_itsa_fct_full_pnl.sql new file mode 100644 index 0000000..2f30a7e --- /dev/null +++ b/Database Backups/SQL Views/vw_bi_itsa_fct_full_pnl.sql @@ -0,0 +1,65 @@ +CREATE OR REPLACE VIEW public.vw_bi_itsa_fct_full_pnl AS + WITH trade AS ( + SELECT pc."intPurchaseLineId" AS "intTradeId", + (pc."intPurchaseLineId" || '_0'::text) AS strtradeid + FROM vw_utility_dim_physical_purchase_contract pc + WHERE ((1 = 1) AND (pc."intPurchaseLineId" > 0)) + UNION ALL + SELECT sc."intSaleLineId" AS "intTradeId", + ('0_'::text || sc."intSaleLineId") AS strtradeid + FROM vw_utility_dim_physical_sale_contract sc + WHERE ((1 = 1) AND (sc."intSaleLineId" > 0)) + ) + SELECT 2 AS "intPnlTypeId", + trade.strtradeid AS "strConformedReferenceId", + contracts."intPriceGroupId", + contracts."Line Description", + contracts."Open Quantity Signed", + contracts."Price", + contracts."Open Cost Amount" AS amount, + contracts."Estimated BL Date" AS "Delivery Date" + FROM (vw_bi_itsa_physical_open_costs contracts + JOIN trade trade ON ((contracts."intTradeLineId" = trade."intTradeId"))) +UNION ALL + SELECT 3 AS "intPnlTypeId", + ((shipment."intPurchaseLineId" || '_'::text) || shipment."intSaleLineId") AS "strConformedReferenceId", + 1 AS "intPriceGroupId", + 'Purchase'::character varying AS "Line Description", + shipment."Shipped Quantity" AS "Open Quantity Signed", + shipment."Purchase Price" AS "Price", + (shipment."Shipped Quantity" * (shipment."Purchase Price")::double precision) AS amount, + shipment."BL Date" AS "Delivery Date" + FROM vw_bi_itsa_fct_shipments shipment +UNION ALL + SELECT 3 AS "intPnlTypeId", + ((shipment."intPurchaseLineId" || '_'::text) || shipment."intSaleLineId") AS "strConformedReferenceId", + 1 AS "intPriceGroupId", + 'Sale'::character varying AS "Line Description", + shipment."Shipped Quantity" AS "Open Quantity Signed", + shipment."Sale Price" AS "Price", + (shipment."Shipped Quantity" * (shipment."Sale Price")::double precision) AS amount, + shipment."BL Date" AS "Delivery Date" + FROM vw_bi_itsa_fct_shipments shipment +UNION ALL + SELECT 3 AS "intPnlTypeId", + ((shipment."intPurchaseLineId" || '_'::text) || shipment."intSaleLineId") AS "strConformedReferenceId", + 2 AS "intPriceGroupId", + fees."Fee" AS "Line Description", + shipment."Shipped Quantity" AS "Open Quantity Signed", + fees."Price", + (shipment."Shipped Quantity" * (fees."Price")::double precision) AS amount, + shipment."BL Date" AS "Delivery Date" + FROM (vw_bi_itsa_fct_shipment_fees fees + JOIN vw_bi_itsa_fct_shipments shipment ON ((fees."intShipmentId" = shipment."intShipmentId"))) +UNION ALL + SELECT 1 AS "intPnlTypeId", + trade."strTradeId" AS "strConformedReferenceId", + 1 AS "intPriceGroupId", + 'Expected Price'::character varying AS "Line Description", + ctr."Contract Quantity" AS "Open Quantity Signed", + ctr."Avg Price" AS "Price", + (ctr."Contract Quantity" * ctr."Avg Price") AS amount, + ctr."Delivery Date" + FROM (vw_bi_itsa_fct_contract ctr + JOIN vw_bi_dim_trade trade ON ((ctr."TradeLineId" = trade."intTradeId"))) + WHERE ((trade."Book")::text ~~ '%Projection%'::text);; diff --git a/Database Backups/SQL Views/vw_bi_itsa_fct_shipments.sql b/Database Backups/SQL Views/vw_bi_itsa_fct_shipments.sql index 308aa64..4d7723b 100644 --- a/Database Backups/SQL Views/vw_bi_itsa_fct_shipments.sql +++ b/Database Backups/SQL Views/vw_bi_itsa_fct_shipments.sql @@ -10,7 +10,9 @@ CREATE OR REPLACE VIEW public.vw_bi_itsa_fct_shipments AS sm."dblQuantity" AS "Shipped Quantity", sm."strUnit" AS "Quantity Unit", COALESCE(p."dblUnitPrice", (0)::numeric) AS "Purchase Price", - COALESCE(s."dblUnitPrice", (0)::numeric) AS "Sale Price" - FROM ((vw_utility_stock_movements sm + COALESCE(s."dblUnitPrice", (0)::numeric) AS "Sale Price", + shipment."dtmBillOfLadingDate" AS "BL Date" + FROM (((vw_utility_stock_movements sm + JOIN vw_utility_shipment_in shipment ON ((sm."intShipmentId" = shipment."intShipmentId"))) LEFT JOIN vw_utility_purchase_physical_contract p ON ((sm."intPurchaseLineId" = p."intPurchaseLineId"))) LEFT JOIN vw_utility_sale_physical_contract s ON ((sm."intSaleLineId" = s."intSaleLineId")));; diff --git a/Database Backups/SQL Views/vw_bi_itsa_monthly_trade_ifrs_adjustments.sql b/Database Backups/SQL Views/vw_bi_itsa_monthly_trade_ifrs_adjustments.sql new file mode 100644 index 0000000..9627651 --- /dev/null +++ b/Database Backups/SQL Views/vw_bi_itsa_monthly_trade_ifrs_adjustments.sql @@ -0,0 +1,7 @@ +CREATE OR REPLACE VIEW public.vw_bi_itsa_monthly_trade_ifrs_adjustments AS + SELECT "intAdjustmentId", + "dtmAdjustmentDate" AS "Adjustment Date", + "dblAmount" AS "Amount", + "strCurrency" AS "Currency", + "strComments" AS "Comments" + FROM vw_utility_monthly_trade_ifrs_adjustments;; diff --git a/Database Backups/SQL Views/vw_bi_itsa_physical_open_costs.sql b/Database Backups/SQL Views/vw_bi_itsa_physical_open_costs.sql new file mode 100644 index 0000000..d22d004 --- /dev/null +++ b/Database Backups/SQL Views/vw_bi_itsa_physical_open_costs.sql @@ -0,0 +1,148 @@ +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" + 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" + 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" + 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" + 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" + 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" + 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));; diff --git a/Database Backups/SQL Views/vw_utility_dim_physical_purchase_contract.sql b/Database Backups/SQL Views/vw_utility_dim_physical_purchase_contract.sql index 3db7dc8..aadb341 100644 --- a/Database Backups/SQL Views/vw_utility_dim_physical_purchase_contract.sql +++ b/Database Backups/SQL Views/vw_utility_dim_physical_purchase_contract.sql @@ -27,7 +27,9 @@ CREATE OR REPLACE VIEW public.vw_utility_dim_physical_purchase_contract AS ''::character varying AS "strBookName", ''::character varying AS "strQuantityUom", ''::character varying AS "strIncotermCode", - NULL::date AS "dtmEstimatedBLDate" + NULL::date AS "dtmEstimatedBLDate", + ''::character varying AS "strPricingType", + ''::character varying AS "strCounterpartyCountry" UNION ALL SELECT pl.id AS "intPurchaseLineId", pc.number AS "strContractNb", @@ -62,7 +64,9 @@ UNION ALL book."strValueName" AS "strBookName", pu1.name AS "strQuantityUom", COALESCE(inc.code, ''::character varying) AS "strIncotermCode", - COALESCE(bl.estimated_date, pl.to_del) AS "dtmEstimatedBLDate" + COALESCE(bl.estimated_date, pl.to_del) AS "dtmEstimatedBLDate", + pl.price_type AS "strPricingType", + COALESCE(ad."strCountry", 'Undefined'::character varying) AS "strCounterpartyCountry" FROM (((((((((((((((((((purchase_line pl JOIN purchase_purchase pc ON ((pl.purchase = pc.id))) JOIN party_party pty ON ((pc.party = pty.id))) diff --git a/Database Backups/SQL Views/vw_utility_dim_physical_sale_contract.sql b/Database Backups/SQL Views/vw_utility_dim_physical_sale_contract.sql index 7de4a81..d797479 100644 --- a/Database Backups/SQL Views/vw_utility_dim_physical_sale_contract.sql +++ b/Database Backups/SQL Views/vw_utility_dim_physical_sale_contract.sql @@ -27,7 +27,9 @@ CREATE OR REPLACE VIEW public.vw_utility_dim_physical_sale_contract AS ''::character varying AS "strStrategyName", ''::character varying AS "strBookName", ''::character varying AS "strIncotermCode", - NULL::date AS "dtmEstimatedBLDate" + NULL::date AS "dtmEstimatedBLDate", + ''::character varying AS "strPricingType", + ''::character varying AS "strCounterpartyCountry" UNION ALL SELECT sl.id AS "intSaleLineId", sc.number AS "strContractNb", @@ -57,7 +59,9 @@ UNION ALL strategy."strValueName" AS "strStrategyName", book."strValueName" AS "strBookName", COALESCE(inc.code, ''::character varying) AS "strIncotermCode", - COALESCE(bl.estimated_date, sl.to_del) AS "dtmEstimatedBLDate" + COALESCE(bl.estimated_date, sl.to_del) AS "dtmEstimatedBLDate", + sl.price_type AS "strPricingType", + COALESCE(ad."strCountry", 'Undefined'::character varying) AS "strCounterpartyCountry" FROM (((((((((((((((((((sale_line sl JOIN sale_sale sc ON ((sl.sale = sc.id))) JOIN party_party pty ON ((sc.party = pty.id))) @@ -77,5 +81,5 @@ UNION ALL LEFT JOIN vw_utility_last_counterparty_address ad ON ((pty.id = ad."intPartyId"))) LEFT JOIN vw_utility_strategy_dimension strategy ON (((sl.id = strategy."intTradeLineId") AND (strategy."strTradeCategory" = 'Physical'::text) AND (strategy."strTradeType" = 'Sale'::text)))) LEFT JOIN vw_utility_book_dimension book ON (((sl.id = book."intTradeLineId") AND (book."strTradeCategory" = 'Physical'::text) AND (book."strTradeType" = 'Sale'::text)))) - LEFT JOIN pricing_estimated bl ON (((sl.id = bl.line) AND ((bl.trigger)::text = 'bldate'::text)))) + LEFT JOIN pricing_estimated bl ON (((sl.id = bl.sale_line) AND ((bl.trigger)::text = 'bldate'::text)))) WHERE ((1 = 1) AND ((sl.type)::text = 'line'::text));; diff --git a/Database Backups/SQL Views/vw_utility_monthly_trade_ifrs_adjustments.sql b/Database Backups/SQL Views/vw_utility_monthly_trade_ifrs_adjustments.sql new file mode 100644 index 0000000..e4126ca --- /dev/null +++ b/Database Backups/SQL Views/vw_utility_monthly_trade_ifrs_adjustments.sql @@ -0,0 +1,8 @@ +CREATE OR REPLACE VIEW public.vw_utility_monthly_trade_ifrs_adjustments AS + SELECT apti.id AS "intAdjustmentId", + apti.date AS "dtmAdjustmentDate", + apti.amount AS "dblAmount", + cur.name AS "strCurrency", + apti.comment AS "strComments" + FROM (account_physical_trade_ifrs apti + JOIN currency_currency cur ON ((cur.id = apti.currency)));; diff --git a/Database Backups/SQL Views/vw_utility_physical_mtm_definition.sql b/Database Backups/SQL Views/vw_utility_physical_mtm_definition.sql new file mode 100644 index 0000000..54756d8 --- /dev/null +++ b/Database Backups/SQL Views/vw_utility_physical_mtm_definition.sql @@ -0,0 +1,46 @@ +CREATE OR REPLACE VIEW public.vw_utility_physical_mtm_definition AS + SELECT 'Purchase'::text AS "strContractType", + pp.id AS "intContractId", + pl.id AS "intContractLineId", + pl.quantity AS "dblLineQuantity", + ms.name AS "strStrategy", + msc.name AS "strScenario", + cur.symbol AS "strStrategyCurrency", + pc_mtm.price_source_type AS "strComponentPriceSource", + pc_mtm.ratio AS "strComponentRatioPct", + ft.name AS "strComponentFixType", + pi.price_desc AS "strComponentCurve", + pc_cal.name AS "strComponentCalendar" + FROM (((((((((purchase_strategy ps + JOIN purchase_line pl ON ((pl.id = ps.line))) + JOIN purchase_purchase pp ON ((pp.id = pl.purchase))) + JOIN mtm_strategy ms ON ((ms.id = ps.strategy))) + JOIN mtm_scenario msc ON ((msc.id = ms.scenario))) + JOIN currency_currency cur ON ((cur.id = ms.currency))) + LEFT JOIN pricing_component pc_mtm ON ((pc_mtm.strategy = ms.id))) + LEFT JOIN price_fixtype ft ON ((ft.id = pc_mtm.fix_type))) + LEFT JOIN price_price pi ON ((pi.id = pc_mtm.price_index))) + LEFT JOIN price_calendar pc_cal ON ((pc_cal.id = pc_mtm.calendar))) +UNION ALL + SELECT 'Sale'::text AS "strContractType", + ss.id AS "intContractId", + sl.id AS "intContractLineId", + sl.quantity AS "dblLineQuantity", + ms.name AS "strStrategy", + msc.name AS "strScenario", + cur.symbol AS "strStrategyCurrency", + pc_mtm.price_source_type AS "strComponentPriceSource", + pc_mtm.ratio AS "strComponentRatioPct", + ft.name AS "strComponentFixType", + pi.price_desc AS "strComponentCurve", + pc_cal.name AS "strComponentCalendar" + FROM (((((((((sale_strategy sa + JOIN sale_line sl ON ((sl.id = sa.sale_line))) + JOIN sale_sale ss ON ((ss.id = sl.sale))) + JOIN mtm_strategy ms ON ((ms.id = sa.strategy))) + JOIN mtm_scenario msc ON ((msc.id = ms.scenario))) + JOIN currency_currency cur ON ((cur.id = ms.currency))) + LEFT JOIN pricing_component pc_mtm ON ((pc_mtm.strategy = ms.id))) + LEFT JOIN price_fixtype ft ON ((ft.id = pc_mtm.fix_type))) + LEFT JOIN price_price pi ON ((pi.id = pc_mtm.price_index))) + LEFT JOIN price_calendar pc_cal ON ((pc_cal.id = pc_mtm.calendar)));; diff --git a/Database Backups/SQL Views/vw_utility_physical_valuation_line.sql b/Database Backups/SQL Views/vw_utility_physical_valuation_line.sql new file mode 100644 index 0000000..870b949 --- /dev/null +++ b/Database Backups/SQL Views/vw_utility_physical_valuation_line.sql @@ -0,0 +1,30 @@ +CREATE OR REPLACE VIEW public.vw_utility_physical_valuation_line AS + SELECT 'Purchase'::text AS "strContractType", + vvl.date AS "dtmValuationDate", + vvl.reference AS "strReference", + vvl.purchase AS "intContractId", + vvl.line AS "intContractLineId", + vvl.lot AS "intLotId", + vvl.quantity AS "dblQuantity", + initcap((vvl.state)::text) AS "strState", + vvl.price AS "dblPrice", + COALESCE(vvl.mtm_price, (0)::numeric) AS "dblMtmPrice", + vvl.amount AS "dblAmount", + vvl.base_amount AS "dblBaseAmount" + FROM valuation_valuation_line vvl + WHERE ((1 = 1) AND ((vvl.type)::text = 'pur. priced'::text) AND (COALESCE(vvl.lot, 0) > 0) AND (COALESCE(vvl.purchase, 0) > 0) AND (COALESCE(vvl.line, 0) > 0)) +UNION ALL + SELECT 'Sale'::text AS "strContractType", + vvl.date AS "dtmValuationDate", + vvl.reference AS "strReference", + vvl.sale AS "intContractId", + vvl.sale_line AS "intContractLineId", + vvl.lot AS "intLotId", + vvl.quantity AS "dblQuantity", + initcap((vvl.state)::text) AS "strState", + vvl.price AS "dblPrice", + COALESCE(vvl.mtm_price, (0)::numeric) AS "dblMtmPrice", + vvl.amount AS "dblAmount", + vvl.base_amount AS "dblBaseAmount" + FROM valuation_valuation_line vvl + WHERE ((1 = 1) AND ((vvl.type)::text = 'sale priced'::text) AND (COALESCE(vvl.lot, 0) > 0) AND (COALESCE(vvl.sale, 0) > 0) AND (COALESCE(vvl.sale_line, 0) > 0));;