Tuning SQL Query in Oracle - sql

I would really appreciate your help in tuning the below SQL query. It kept running for 10 mins when I cancelled it.
MARC_SEL gave me 31,253 records in 52 seconds
and MVKE_SEL gave me 431,060 records in 22 seconds
I refactored it to use with clause but nothing much changed.What else can I incorporate to make it faster. Please help.
WITH ALL_XSAP_MATNR
AS (SELECT DISTINCT XSAP.MATNR,XSAP.MTART,XSAP.SOURCE FROM XXX_MAIN.XXX_XSAP XSAP
WHERE SOURCE = 'SP' )
, MARC_SEL AS
( SELECT DISTINCT A.SOURCE
,MARA.MATNR
,MARA.MTART
,MARA.MBRSH
,MARC.WERKS
,NVL(PX.WERKS,'/') DWERK
,NVL(MBEW.HKMAT,'/') HKMAT
,NVL(MBEW.EKALR,'/') EKALR
,NVL(MARC.STAWN,'/') STAWN
FROM ALL_XSAP_MATNR A
, XXX_MAIN.XXX_SAP_MARA MARA
, XXX_MAIN.XXX_SAP_MARC MARC
, XXX_MAIN.XXX_MP_WERKS_PLANT_XREF PX
, XXX_MAIN.XXX_SAP_MBEW MBEW
WHERE A.MATNR = MARA.MATNR
AND A.MTART = MARA.MTART
AND MARA.MATNR = MARC.MATNR
AND MARC.MATNR = MBEW.MATNR
AND MARC.WERKS = MBEW.BWKEY
AND PX.LEGACY_PLANT = MARC.WERKS
AND PX.SOURCE = 'SP'
)
, MVKE_SEL AS
( SELECT DISTINCT
MVKE.MATNR
,'/' LEGACY_ORG
,'/' LEGACY_MATNR
,NVL(MX_VKORG.SAP_DE_VAL,'/') VKORG
,NVL(SUBSTR(MX_VKORG.SAP_DE,6,2),'/') VTWEG
-- ,NVL(TVRKME.MSEH3,'/') VRKME
,NVL(MVKE.KONDM,'/') KONDM
,NVL(MVKE.VERSG,'/') VERSG
,'/' IPRKZ
,'/' MHDRZ,NVL(MVKE.VMSTA,'/') VMSTA
,NVL(TO_CHAR(MVKE.VMSTD ,'YYYYMMDD' ),'/') VMSTD
,NVL(MVKE.PMATN,'/') PMATN
,NVL(MVKE.MVGR2,'/') MVGR2
,NVL(MVKE.MVGR3,'/') MVGR3
,NVL(MVKE.VAVME,'/') VAVME
,'/' MVGR4
,'/' MVGR5
,NVL(MVKE.MTPOS,'/') MTPOS
,NVL(MVKE.PRAT1,'/') PRAT1
,NVL(MVKE.SKTOF,'/') SKTOF
,'/' AUMNG
,NVL(MVKE.PRODH,'/') PRODH
,'/' MVGR1
,NVL(MVKE.KTGRM,'/') KTGRM
,MX_VKORG.DESC4
FROM XXX_MAIN.XXX_SAP_MVKE MVKE
, XXX_MAIN.XXX_MP_VKVT_XREF MX_VKORG
WHERE MX_VKORG.SOURCE_DE_VAL = MVKE.VKORG
AND SUBSTR(MX_VKORG.SAP_DE,6,2) = MVKE.VTWEG
AND MX_VKORG.SOURCE_TBL = 'SP'
AND MX_VKORG.SOURCE_DE = 'MVKE'
AND SUBSTR(MX_VKORG.SAP_DE,1,5)= 'VKORG'
AND MX_VKORG.DESC2 IS NULL )
SELECT DISTINCT
MARC.SOURCE
,MARC.MATNR
,MARC.MTART
,MARC.MBRSH
,MARC.WERKS
,MARC.DWERK
,MARC.HKMAT
,MARC.EKALR
,MARC.STAWN
,MVKE.LEGACY_ORG
,MVKE.LEGACY_MATNR
,MVKE.VKORG
,MVKE.VTWEG
,MVKE.KONDM
,MVKE.VERSG
,MVKE.VMSTA
,MVKE.VMSTD
,MVKE.PMATN
,MVKE.MVGR2
,MVKE.MVGR3
,MVKE.VAVME
,MVKE.MTPOS
,MVKE.PRAT1
,MVKE.SKTOF
,MVKE.PRODH
,MVKE.KTGRM
FROM MARC_SEL MARC
, MVKE_SEL MVKE
WHERE MARC.MATNR = MVKE.MATNR
AND MARC.WERKS = MVKE.DESC4

Start by simplifying your query as you do not need to do multiple DISTINCTs (it's only necessary in the final output) and you are selecting many columns that you are not outputting. You are also joining some tables which you are not selecting from and if there are multiple matching rows for these then it may generate duplicate rows - using something like EXISTS can eliminate these joins.
Like this:
WITH MARC_SEL AS (
SELECT A.SOURCE,
MARA.MATNR,
MARC.WERKS
FROM XXX_MAIN.XXX_XSAP A
INNER JOIN XXX_MAIN.XXX_SAP_MARA MARA
ON ( A.MATNR = MARA.MATNR
AND A.MTART = MARA.MTART )
INNER JOIN XXX_MAIN.XXX_SAP_MARC MARC
ON ( MARA.MATNR = MARC.MATNR )
WHERE EXISTS( SELECT 'X'
FROM XXX_MAIN.XXX_MP_WERKS_PLANT_XREF PX
WHERE PX.LEGACY_PLANT = MARC.WERKS
AND PX.SOURCE = 'SP' )
AND EXISTS( SELECT 'X'
FROM XXX_MAIN.XXX_SAP_MBEW MBEW
WHERE MARC.MATNR = MBEW.MATNR
AND MARC.WERKS = MBEW.BWKEY )
AND A.SOURCE = 'SP'
)
, MVKE_SEL AS (
SELECT NVL(MX_VKORG.SAP_DE_VAL,'/') VKORG,
NVL(SUBSTR(MX_VKORG.SAP_DE,6,2),'/') VTWEG,
MX_VKORG.DESC4
FROM XXX_MAIN.XXX_MP_VKVT_XREF MX_VKORG
WHERE EXISTS ( SELECT 'X'
FROM XXX_MAIN.XXX_SAP_MVKE MVKE
WHERE MX_VKORG.SOURCE_DE_VAL = MVKE.VKORG
AND SUBSTR(MX_VKORG.SAP_DE,6,2) = MVKE.VTWEG )
AND MX_VKORG.SOURCE_TBL = 'SP'
AND MX_VKORG.SOURCE_DE = 'MVKE'
AND SUBSTR(MX_VKORG.SAP_DE,1,5)= 'VKORG'
AND MX_VKORG.DESC2 IS NULL
)
SELECT DISTINCT
MARC.SOURCE,
MARC.MATNR,
MVKE.VKORG,
MARC.WERKS,
MVKE.VTWEG
FROM MARC_SEL MARC
INNER JOIN MVKE_SEL MVKE
ON ( MARC.MATNR = MVKE.MATNR
AND MARC.WERKS = MVKE.DESC4 )

Added hints to subqueries and it came back in a minute.
WITH ALL_XSAP_MATNR
AS (SELECT /*+ materialize */ DISTINCT XSAP.MATNR,XSAP.MTART,XSAP.SOURCE FROM XXX_MAIN.XXX_XSAP XSAP
WHERE SOURCE = 'SP' )
, MARC_SEL AS
( SELECT /*+ materialize */ DISTINCT A.SOURCE
,MARA.MATNR
,MARA.MTART
,MARA.MBRSH
,MARC.WERKS
,NVL(PX.WERKS,'/') DWERK
,NVL(MBEW.HKMAT,'/') HKMAT
,NVL(MBEW.EKALR,'/') EKALR
,NVL(MARC.STAWN,'/') STAWN
FROM ALL_XSAP_MATNR A
, XXX_MAIN.XXX_SAP_MARA MARA
, XXX_MAIN.XXX_SAP_MARC MARC
, XXX_MAIN.XXX_MP_WERKS_PLANT_XREF PX
, XXX_MAIN.XXX_SAP_MBEW MBEW
WHERE A.MATNR = MARA.MATNR
AND A.MTART = MARA.MTART
AND MARA.MATNR = MARC.MATNR
AND MARC.MATNR = MBEW.MATNR
AND MARC.WERKS = MBEW.BWKEY
AND PX.LEGACY_PLANT = MARC.WERKS
AND PX.SOURCE = 'SP'
)
, MVKE_SEL AS
( SELECT /*+ materialize */ DISTINCT
MVKE.MATNR
,'/' LEGACY_ORG
,'/' LEGACY_MATNR
,NVL(MX_VKORG.SAP_DE_VAL,'/') VKORG
,NVL(SUBSTR(MX_VKORG.SAP_DE,6,2),'/') VTWEG
-- ,NVL(TVRKME.MSEH3,'/') VRKME
,NVL(MVKE.KONDM,'/') KONDM
,NVL(MVKE.VERSG,'/') VERSG
,'/' IPRKZ
,'/' MHDRZ,NVL(MVKE.VMSTA,'/') VMSTA
,NVL(TO_CHAR(MVKE.VMSTD ,'YYYYMMDD' ),'/') VMSTD
,NVL(MVKE.PMATN,'/') PMATN
,NVL(MVKE.MVGR2,'/') MVGR2
,NVL(MVKE.MVGR3,'/') MVGR3
,NVL(MVKE.VAVME,'/') VAVME
,'/' MVGR4
,'/' MVGR5
,NVL(MVKE.MTPOS,'/') MTPOS
,NVL(MVKE.PRAT1,'/') PRAT1
,NVL(MVKE.SKTOF,'/') SKTOF
,'/' AUMNG
,NVL(MVKE.PRODH,'/') PRODH
,'/' MVGR1
,NVL(MVKE.KTGRM,'/') KTGRM
,MX_VKORG.DESC4
FROM XXX_MAIN.XXX_SAP_MVKE MVKE
, XXX_MAIN.XXX_MP_VKVT_XREF MX_VKORG
WHERE MX_VKORG.SOURCE_DE_VAL = MVKE.VKORG
AND SUBSTR(MX_VKORG.SAP_DE,6,2) = MVKE.VTWEG
AND MX_VKORG.SOURCE_TBL = 'SP'
AND MX_VKORG.SOURCE_DE = 'MVKE'
AND SUBSTR(MX_VKORG.SAP_DE,1,5)= 'VKORG'
AND MX_VKORG.DESC2 IS NULL )
SELECT DISTINCT /*+ use_hash(MARC,MVKE ) */
MARC.SOURCE
,MARC.MATNR
,MARC.MTART
,MARC.MBRSH
,MARC.WERKS
,MARC.DWERK
,MARC.HKMAT
,MARC.EKALR
,MARC.STAWN
,MVKE.LEGACY_ORG
,MVKE.LEGACY_MATNR
,MVKE.VKORG
,MVKE.VTWEG
,MVKE.KONDM
,MVKE.VERSG
,MVKE.VMSTA
,MVKE.VMSTD
,MVKE.PMATN
,MVKE.MVGR2
,MVKE.MVGR3
,MVKE.VAVME
,MVKE.MTPOS
,MVKE.PRAT1
,MVKE.SKTOF
,MVKE.PRODH
,MVKE.KTGRM
FROM MARC_SEL MARC
, MVKE_SEL MVKE
WHERE MARC.MATNR = MVKE.MATNR
AND MARC.WERKS = MVKE.DESC4

Related

Converting Oracle Query to Snowflake

Please need some help to convert the below oracle code to Snowflake. When I m trying this, facing invalid identifier, rownum,
SELECT Customer_Id
,Release_type
,Customer_Name
,XYZ_Product_Name
,XYZ_Product_Salesforce_Number
,XYZ_Product_Code
,XYZ_Product_Type
,Brand_Family
,Qty_Purchased
,qty_u2dt
,sbc_Term_Start_Date
,sbc_Term_End_Date
,Months_Sold
,Months_Used
,Remaining_Months
,round(decode(Months_Sold, 0, 0, (Months_Used / Months_Sold) * 100), 2) AS Term_used_perc
,round(Actual_monthly_Usage, 2) AS Actual_monthly_Usage
,round((Actual_monthly_Usage * Remaining_Months) + qty_u2dt, 2) AS projected_usage
,round(decode(Qty_Purchased, 0, 0, (((Actual_monthly_Usage * Remaining_Months) + qty_u2dt) / Qty_Purchased) * 100), 2) AS projected_usage_perc
,round((((Actual_monthly_Usage * Remaining_Months) + qty_u2dt) - Qty_Purchased), 2) AS projected_over_under_usage
,CASE
WHEN round(decode(Qty_Purchased, 0, 0, (((Actual_monthly_Usage * Remaining_Months) + qty_u2dt) / Qty_Purchased) * 100), 2) = 100
AND Qty_Purchased = qty_u2dt
THEN 'Resell'
WHEN round(decode(Qty_Purchased, 0, 0, (((Actual_monthly_Usage * Remaining_Months) + qty_u2dt) / Qty_Purchased) * 100), 2) < 100
THEN 'Churn'
WHEN round(decode(Qty_Purchased, 0, 0, (((Actual_monthly_Usage * Remaining_Months) + qty_u2dt) / Qty_Purchased) * 100), 2) > 100
THEN 'Upsell'
WHEN round(decode(Qty_Purchased, 0, 0, (((Actual_monthly_Usage * Remaining_Months) + qty_u2dt) / Qty_Purchased) * 100), 2) = 100
AND Qty_Purchased = (round((Actual_monthly_Usage * Remaining_Months) + qty_u2dt))
THEN 'On Track'
ELSE 'Unknown'
END Projected_Indicator
,Timeline
,Contr_End_Date
,Salesforce_Account_Number
,Salesforce_csa_Id
,Salesforce_Contact
,Product_Family_Description
,Product_Family_Code
,Product_Brand_Description
,Product_Brand_Code
,Product_Category_Description
,Product_Category_Code
,Product_Name
,Product_Code
,product_type
,s_2_CUSTOMER_OWNER_NID
,G_Customer_Number
,G_Customer_Name
,G_Customer_Address
,G_Customer_City
,G_Customer_Country
,G_Customer_Phone
,G_Customer_State_Prov
,G_Customer_Zip_Code
,G_Customer_SFDC_NID
,G_Customer_SFDC_NID_Link
,s_2_Customer_Number
,s_2_Customer_Name
,s_2_Customer_Address
,s_2_Customer_City
,s_2_Customer_Country
,s_2_Customer_Phone
,s_2_Customer_State_Prov
,s_2_Customer_Zip_Code
,s_2_Customer_Health_Status
,s_2_Customer_SFDC_NID
,s_2_Customer_SFDC_NID_Link
,s_2_Customer_XYZ_NID
,s_2_Customer_XYZ_NID_Link
,s_2_Customer_Sales_Owner
,s_2_Customer_CSM
,s_2_Customer_CSM_Manager
,s_2_Customer_Sales_Manager
,s_2_customer_sales_team
,s_2_Customer_PSM
,s_2_Customer_PSM_Manager
,sbc_number
,sbc_status
,subscriptiontype
,sbc_start_date
,sbc_end_date
,sbc_termination_date
,sbc_SFDC_NID
,sbc_SFDC_NID_Link
,istestaccount
,child_sub_name
,parent_sub_name
,requiredbyid
,Parent_PRODUCTID
,parent_productname
,Initial_recurrinng_term
,Recurrinng_reolover_term
,renewal_date
,rate_set_uom
,Unit_of_Measure
,Contract_Number
,Contract_SFDC_NID
,Contract_SFDC_NID_Link
,Contract_Start_Date
,Contract_End_Date
FROM (
SELECT Customer_Id
,Release_type
,Customer_Name
--,Contract_Number
,XYZ_Product_Name
,XYZ_Product_Salesforce_Number
,XYZ_Product_Code
,XYZ_Product_Type
,Brand_Family
,Qty_Purchased
,qty_u2dt
,sbc_Term_Start_Date
,sbc_Term_End_Date
,round(months_between(sbc_Term_End_Date, sbc_Term_Start_Date), 2) AS Months_Sold
,round(months_between(current_date, sbc_Term_Start_Date), 2) AS Months_Used
,(round(months_between(sbc_Term_End_Date, sbc_Term_Start_Date), 2)) - (round(months_between(current_date, sbc_Term_Start_Date), 2)) AS Remaining_Months
,decode(months_between(current_date, sbc_Term_Start_Date), 0, 0, ((qty_u2dt / months_between(current_date, sbc_Term_Start_Date)))) AS Actual_monthly_Usage
,CASE
WHEN sbc_Term_End_Date < current_date
THEN 'Past'
WHEN sbc_Term_Start_Date > current_date
THEN 'Future'
ELSE 'Present'
END Timeline
,Contr_End_Date
,Salesforce_Account_Number
,Salesforce_csa_Id
,Salesforce_Contact
,Product_Family_Description
,Product_Family_Code
,Product_Brand_Description
,Product_Brand_Code
,Product_Category_Description
,Product_Category_Code
,Product_Name
,Product_Code
,product_type
,s_2_CUSTOMER_OWNER_NID
,G_Customer_Number
,G_Customer_Name
,G_Customer_Address
,G_Customer_City
,G_Customer_Country
,G_Customer_Phone
,G_Customer_State_Prov
,G_Customer_Zip_Code
,G_Customer_SFDC_NID
,G_Customer_SFDC_NID_Link
,s_2_Customer_Number
,s_2_Customer_Name
,s_2_Customer_Address
,s_2_Customer_City
,s_2_Customer_Country
,s_2_Customer_Phone
,s_2_Customer_State_Prov
,s_2_Customer_Zip_Code
,s_2_Customer_Health_Status
,s_2_Customer_SFDC_NID
,s_2_Customer_SFDC_NID_Link
,s_2_Customer_XYZ_NID
,s_2_Customer_XYZ_NID_Link
,s_2_Customer_Sales_Owner
,s_2_Customer_CSM
,s_2_Customer_CSM_Manager
,s_2_Customer_Sales_Manager
,s_2_customer_sales_team
,s_2_Customer_PSM
,s_2_Customer_PSM_Manager
,sbc_number
,sbc_status
,subscriptiontype
,sbc_start_date
,sbc_end_date
,sbc_termination_date
,sbc_SFDC_NID
,sbc_SFDC_NID_Link
,istestaccount
,child_sub_name
,parent_sub_name
,requiredbyid
,Parent_PRODUCTID
,parent_productname
,Initial_recurrinng_term
,Recurrinng_reolover_term
,renewal_date
,rate_set_uom
,Unit_of_Measure
,Contract_Number
,Contract_SFDC_NID
,Contract_SFDC_NID_Link
,Contract_Start_Date
,Contract_End_Date
FROM (
SELECT DISTINCT cust.Id AS Customer_Id
,sub.rtpname AS Release_type
,org.Name AS Customer_Name
,prod.Name AS XYZ_Product_Name
,prod.SalesforceId AS XYZ_Product_Salesforce_Number
,prod.Code AS XYZ_Product_Code
,prod.Type AS XYZ_Product_Type
,pb.Name AS Brand_Family
,cntr.CURRENCYISOCODE AS currency_code
,nvl((
SELECT *
FROM (
SELECT to_char(cliterm.Quantity)
FROM XYZ_ContractLineItemTerm cliterm
WHERE cliterm.ContractLineItem_id = cli.Id
AND (
cliterm.EndDate IS NULL
OR cliterm.EndDate > add_months(current_date, - 3)
)
AND cliterm.PriceRuleItem_id IS NULL
ORDER BY cli.id DESC
)
WHERE rownum = 1
), cli.Quantity) AS Qty_Purchased
,nvl((
SELECT *
FROM (
SELECT cliterm.UsedQuantity
FROM XYZ_ContractLineItemTerm cliterm
WHERE cliterm.ContractLineItem_id = cli.Id
AND (
cliterm.EndDate IS NULL
OR cliterm.EndDate > add_months(current_date, - 3)
)
AND cliterm.PriceRuleItem_id IS NULL
ORDER BY cli.id DESC
)
WHERE rownum = 1
), cli.UsedQuantity) AS qty_u2dt
,nvl((
SELECT *
FROM (
SELECT cliterm.StartDate
FROM XYZ_ContractLineItemTerm cliterm
WHERE cliterm.ContractLineItem_id = cli.Id
AND (
cliterm.EndDate IS NULL
OR cliterm.EndDate > add_months(current_date, - 3)
)
AND cliterm.PriceRuleItem_id IS NULL
ORDER BY cli.id DESC
)
WHERE rownum = 1
), cli.StartDate) AS sbc_Term_Start_Date
,nvl((
SELECT *
FROM (
SELECT cliterm.EndDate
FROM XYZ_ContractLineItemTerm cliterm
WHERE cliterm.ContractLineItem_id = cli.Id
AND (
cliterm.EndDate IS NULL
OR cliterm.EndDate > add_months(current_date, - 3)
)
AND cliterm.PriceRuleItem_id IS NULL
ORDER BY id DESC
)
WHERE rownum = 1
), cli.EndDate) AS sbc_Term_End_Date
,nvl(to_char(cli.EndDate), (
CASE
WHEN (
cli.StartDate IS NOT NULL
AND con.InitialTerm > 0
)
THEN 'Auto Renewal'
ELSE ''
END
)) AS Contr_End_Date
,cust.SalesforceAccountNumber AS Salesforce_Account_Number
,cust.SalesforceId AS Salesforce_csa_Id
,'s12345' || con.SalesforceId || '/view' AS Salesforce_Contact
,prod_fam.product_family_code AS product_family_code
,prd.Product_Family__c AS Product_Family_Description
,prd.Brand_Code__c AS Product_Brand_Code
,prd.Product_Brand__c AS Product_Brand_Description
,prd.Category_Code__c AS Product_Category_Code
,prd.Product_Category__c AS Product_Category_Description
,prd.name AS Product_Name
,prd.productcode AS Product_Code
,prd.PRODUCT_TYPE__C AS product_type
,dasruler.id AS s_2_CUSTOMER_OWNER_NID
,rupa.global_ultimate_d_u_n_s_number AS G_Customer_Number
,rupa.name AS G_Customer_Name
,rupa.billingstreet AS G_Customer_Address
,rupa.billingcity AS G_Customer_City
,rupa.billingcountry AS G_Customer_Country
,rupa.phone AS G_Customer_Phone
,rupa.billingstate AS G_Customer_State_Prov
,rupa.billingpostalcode AS G_Customer_Zip_Code
,rupa.id AS G_Customer_SFDC_NID
,'12345'|| rupa.id || '/view' AS G_Customer_SFDC_NID_Link
,csa.csa__c AS s_2_Customer_Number
,csa.name AS s_2_Customer_Name
,csa.account_address__c AS s_2_Customer_Address
,csa.account_city__c AS s_2_Customer_City
,csa.account_country__c AS s_2_Customer_Country
,csa.phone AS s_2_Customer_Phone
,csa.account_state_province__c AS s_2_Customer_State_Prov
,csa.account_zip_code__c AS s_2_Customer_Zip_Code
,csa.id AS s_2_Customer_SFDC_NID
,'12345' || csa.id || '/view' AS s_2_Customer_SFDC_NID_Link
,csa.client_health_status__c s_2_Customer_Health_Status
,cust.id AS s_2_Customer_XYZ_NID
,'q12345' || cust.id || '/view' AS s_2_Customer_XYZ_NID_Link
,nvl(dasruler.firstname || ' ' || dasruler.lastname, 'UNKNOWN') AS s_2_Customer_Sales_Owner
,nvl(csacsm.csm, 'UNKNOWN') AS s_2_Customer_CSM
,nvl(csm_mgr.NAME, 'NAVL') AS s_2_Customer_CSM_Manager
,nvl(csasalesmanager.firstname || ' ' || csasalesmanager.lastname, 'UNKNOWN') AS s_2_Customer_Sales_Manager
,nvl(csasalesmanager.USER_SEGMENT__C, 'UNKNOWN') AS s_2_customer_sales_team
,nvl(csapsm.psm, 'NAVL') AS s_2_Customer_PSM
,nvl(psm_mgr.name, 'NAVL') AS s_2_Customer_PSM_Manager
,sub.name AS sbc_number
,sub.STATUS__C AS sbc_status
,sub.sbqq__subscriptiontype__c AS subscriptiontype
,nvl(sub.sbqq__subscriptionstartdate__c, sub.sbqq__startdate__c) AS sbc_start_date
,sub.SBQQ__ENDDATE__C AS sbc_end_date
,sub.sbqq__terminateddate__c AS sbc_termination_date
,sub.annual_recurring_total__c AS annual_recurring_total__c
,sub.one_time_total__c AS one_time_total__c
,sub.currencyisocode AS Localcurrency
,sub.id AS sbc_SFDC_NID
,'z12345' || sub.id || '/view' AS sbc_SFDC_NID_Link
,sub.name AS child_sub_name
,sub.RECURRING_INITIAL_TERM__C AS Initial_recurrinng_term
,sub.RECURRING_ROLLOVER_TERM__C AS Recurrinng_reolover_term
,sub.RENEWAL_DATE__C AS renewal_date
,pasub.name AS parent_sub_name
,sub.sbqq__requiredbyid__c AS requiredbyid
,pasub.SBQQ__PRODUCTID__C AS Parent_PRODUCTID
,pasub.SBQQ__PRODUCTNAME__C AS Parent_Productname
,csa.is_test_account__c AS istestaccount
,prd.rate_set_uom__C AS rate_set_uom
,PRD.Quantity_unit_of_measure__C AS Unit_of_Measure
,cntr.CONTRACTNUMBER AS Contract_Number
,cntr.id AS Contract_SFDC_NID
,'c123456'|| cntr.id || '/view' AS Contract_SFDC_NID_Link
,cntr.startdate AS Contract_Start_Date
,cntr.enddate AS Contract_End_Date
,cntr.annual_recurring_total__c AS Contract_annual_recurring_amt
,cntr.one_time_total__c AS Contract_one_time_total
,cntr.initial_term_total__c AS First_year_contract_value
FROM XYZ_ContractLineItem cli
INNER JOIN XYZ_Contract con ON cli.Contract_id = con.Id
INNER JOIN XYZ_Customer cust ON con.Customer_id = cust.Id
INNER JOIN XYZ_Organization org ON org.Customer_id = cust.Id
INNER JOIN XYZ_Product prod ON cli.Product_id = prod.Id
INNER JOIN XYZ_ProductBrand pb ON prod.ProductBrand_id = pb.Id
LEFT JOIN sfacc csa ON cust.SalesforceAccountNumber = csa.csa__c
LEFT JOIN sfacc rupa ON csa.parentid = rupa.id
LEFT JOIN sf_SBQQ__sbc__C sub ON sub.id = cli.salesforceid
LEFT JOIN SF_SBQQ__sbc__C pasub ON pasub.id = sub.sbqq__requiredbyid__c
LEFT JOIN contractor cntr ON cntr.contractnumber = con.ContractNumber
LEFT JOIN sfu dasruler ON csa.ownerid = dasruler.id
LEFT JOIN sfu dassalesmgr ON dasruler.managerid = dassalesmgr.id
LEFT JOIN (
SELECT f.accountid
,g.managerid
,max(g.NAME) CSM
,max(g.id) CSMID
FROM sfatm f
JOIN sfu g ON f.userid = g.id
WHERE f.teammemberrole = 'AGM'
GROUP BY f.accountid
,g.managerid
) dasagm ON dasagm.accountid = csa.id
LEFT JOIN sfu csm_mgr ON dasagm.managerid = csm_mgr.id
LEFT JOIN (
SELECT f.accountid
,g.managerid
,max(g.NAME) PSM
,max(g.id) CSMID
FROM sfatm f
JOIN sfu g ON f.userid = g.id
WHERE f.teammemberrole = 'SDM'
GROUP BY f.accountid
,g.managerid
) dassdm ON dassdm.accountid = csa.id
LEFT JOIN sfu psm_mgr ON dassdm.managerid = psm_mgr.id
LEFT JOIN SFP prd ON prod.Code = prd.productcode
LEFT JOIN IPFam prod_fam ON prod_fam.product_family_desc = prd.Product_Family__c
WHERE cli.RateEffectiveStatus = 'A'
AND cli.STATUS = 'A'
AND cli.Active = 1
AND con.STATUS IN ('A')
AND CUST.STATUS = 'active'
AND prod.type <> 'KING'
AND csa.itac = 0
)
WHERE Qty_Purchased <> 0
);
Snowflake doesn't have a ROWNUM keyword as Oracle does.
If you want to use that functionality, you can generate an equivalent using the window function, row_number(). This would typically be done by inserting that window function into the bottom level of your query.

Nested in line view Add Column

I have to add a column to this report - the new column name is: uda_value_desc
The code below will give me a result if I enter a PM_ITEM number, I need to insert the code into one of the in line views.
Any help would be appreciated
WITH item_temp AS (SELECT im.item
,uv.uda_value_desc uda_value_desc -----
FROM item_master im
,TABLE(mff_report.parse_strings(:PM_item)) t
, uda_item_lov uil
, uda_values uv
WHERE t.Column_Value = uil.item
and uil.UDA_ID = 511
and uv.uda_id = uil.uda_id
and uv.uda_value = uil.uda_value
and im.item = uil.item
UNION ALL
SELECT im.item
,null
FROM item_master im
,TABLE(mff_report.parse_strings(:PM_item)) t
WHERE t.COLUMN_VALUE = im.item_parent
AND im.item_level = im.tran_level
UNION ALL
SELECT sd.item
,null
FROM skulist_detail sd
,TABLE(mff_report.parse_strings(:PM_item_list)) t
WHERE t.COLUMN_VALUE = sd.skulist
UNION ALL
SELECT ia.item
,null
FROM mffecom.item_attr ia
,TABLE(mff_report.parse_strings(:PM_product_id)) t
WHERE t.COLUMN_VALUE = ia.product_id
UNION ALL
SELECT im.item
,null
FROM item_master im
WHERE :PM_item IS NULL
AND :PM_item_list IS NULL
AND :PM_product_id IS NULL )
SELECT v_item.item_parent
,uda_value_desc
,v_item.item
,mff_report.mff_merch_sql.get_brand_name(v_item.item) brand_name
,v_item.item_desc
,v_selling.product_id
,v_product.product_web_desc
,v_product.product_template
,v_product.romance_copy
,v_selling.selling_point_1
,v_selling.selling_point_2
,v_selling.selling_point_3
,v_selling.selling_point_4
,v_selling.selling_point_5
,v_selling.selling_point_6
,v_selling.selling_point_7
,v_selling.selling_point_8
,v_selling.selling_point_9
,v_selling.selling_point_10
,v_selling.selling_point_11
,v_selling.selling_point_12
,v_selling.selling_point_13
,v_selling.selling_point_14
,v_selling.selling_point_15
,v_selling.selling_point_16
,v_item.vpn
,v_item.diff_1
,v_item.diff1_desc
,v_item.diff_2
,v_item.diff2_desc
,v_item.diff_3
,v_item.diff3_desc
,v_item.diff_4
,v_item.diff4_desc
,v_item.supplier
,v_item.sup_name
,v_product.code_desc fulfillment_method_desc
,v_product.air_ship_restrict
,v_product.do_not_freeze
,v_product.free_shipping
,v_product.refrigerate
,v_product.serial_reqd
,v_product.vaccination
,v_product.is_consumable
,v_product.zero_weight
,v_product.restrict_state
,v_product.no_of_alt_image
,v_product.product_status
,v_product.item_ecom_status
,TO_CHAR(v_product.deactivate_date, 'MM/DD/YYYY') product_deactivate_date
,TO_CHAR(v_product.product_activate_date, 'MM/DD/YYYY') product_activate_date
,TO_CHAR(v_product.item_activate_date, 'MM/DD/YYYY') item_activate_date
,TO_CHAR(v_product.item_deactivate_date, 'MM/DD/YYYY') item_deactivate_date
,v_product.prod_created_by
,TO_CHAR(v_product.prod_created_date, 'MM/DD/YYYY') prod_created_date
,v_product.prod_updated_by
,v_product.item_created_by
,TO_CHAR(v_product.item_created_date, 'MM/DD/YYYY') item_created_date
,v_product.item_updated_by
,v_item.delete_type purge
,v_item.dept
,v_item.class
,v_item.subclass
,mff_orders_sql.buyer_for_item(v_item.item) buyer
,mff_orders_sql.buyer_name_for_item(v_item.item) buyer_name
,v_item.soh
,v_item.length
,v_item.width
,v_item.height
,v_item.weight
,v_variant.variant_id1
,v_variant.value_id1
,v_variant.variant_id2
,v_variant.value_id2
,v_variant.variant_id3
,v_variant.value_id3
,v_variant.variant_id4
,v_variant.value_id4
,v_variant.variant_id5
,v_variant.value_id5
,v_variant.variant_id6
,v_variant.value_id6
FROM (SELECT ia.item
,v_sell.product_id
,MIN(DECODE(row_num,1,selling_point)) selling_point_1
,MIN(DECODE(row_num,2,selling_point)) selling_point_2
,MIN(DECODE(row_num,3,selling_point)) selling_point_3
,MIN(DECODE(row_num,4,selling_point)) selling_point_4
,MIN(DECODE(row_num,5,selling_point)) selling_point_5
,MIN(DECODE(row_num,6,selling_point)) selling_point_6
,MIN(DECODE(row_num,7,selling_point)) selling_point_7
,MIN(DECODE(row_num,8,selling_point)) selling_point_8
,MIN(DECODE(row_num,9,selling_point)) selling_point_9
,MIN(DECODE(row_num,10,selling_point)) selling_point_10
,MIN(DECODE(row_num,11,selling_point)) selling_point_11
,MIN(DECODE(row_num,12,selling_point)) selling_point_12
,MIN(DECODE(row_num,13,selling_point)) selling_point_13
,MIN(DECODE(row_num,14,selling_point)) selling_point_14
,MIN(DECODE(row_num,15,selling_point)) selling_point_15
,MIN(DECODE(row_num,16,selling_point)) selling_point_16
FROM mffecom.item_attr ia
,(SELECT sp.product_id
,row_number () OVER (PARTITION BY sp.product_id ORDER BY sp.selling_point_id) row_num
,sp.selling_point
FROM mffecom.selling_point sp
ORDER BY sp.product_id
,sp.selling_point) v_sell
WHERE ia.product_id = v_sell.product_id
GROUP BY ia.item
,v_sell.product_id) v_selling
,(SELECT NVL(im.item_parent,im.item) item_parent
,im.item
,im.item_desc
,im.dept
,im.class
,im.subclass
,isupp.supplier
,s.sup_name
,isupp.vpn
,miim.stock_on_hand soh
,iscd.length
,iscd.width
,iscd.height
,iscd.weight
,im.diff_1
,di.diff_desc diff1_desc
,im.diff_2
,di2.diff_desc diff2_desc
,im.diff_3
,di3.diff_desc diff3_desc
,im.diff_4
,di4.diff_desc diff4_desc
,dp.delete_type
FROM item_supplier isupp
,sups s
,item_supp_country_dim iscd
,item_master im
,diff_ids di
,diff_ids di2
,diff_ids di3
,diff_ids di4
,daily_purge dp
,merch_item_inv_mv miim
WHERE isupp.item = im.item
AND isupp.item = miim.item (+)
AND isupp.supplier = s.supplier
AND isupp.item = iscd.item
AND isupp.supplier = iscd.supplier
AND isupp.primary_supp_ind = 'Y'
AND iscd.dim_object = 'EA'
AND im.item_level = im.tran_level
AND im.sellable_ind = 'Y'
AND im.diff_1 = di.diff_id (+)
AND im.diff_2 = di2.diff_id (+)
AND im.diff_3 = di3.diff_id (+)
AND im.diff_4 = di4.diff_id (+)
AND im.item = dp.key_value (+)) v_item
,(SELECT ia.item
,pm.product_id
,pm.product_web_desc
,pm.template product_template
,pm.romance_copy
,ia.air_ship_restrict
,ia.do_not_freeze
,ia.free_shipping
,ia.refrigerate
,ia.serial_reqd
,ia.vaccination
,ia.is_consumable
,ia.zero_weight
,pm.no_of_alt_image
,pm.status product_status
,ia.status item_ecom_status
,pm.deactivate_date deactivate_date
,pm.activate_date product_activate_date
,ia.activate_date item_activate_date
,ia.deactivate_date item_deactivate_date
,pm.created_by prod_created_by
,pm.create_datetime prod_created_date
,pm.updated_by prod_updated_by
,ita.created_by item_created_by
,ita.create_date item_created_date
,ia.updated_by item_updated_by
,str.restrict_state
,cd.code_desc
FROM MFFECOM.product_master pm
,MFFECOM.item_attr ia
,MFFECOM.ship_to_restrict str
,rms13.mff_brand mb
,rms13.item_attributes ita
,rms13.code_detail cd
WHERE ia.product_id = pm.product_id
AND ia.item = str.item (+)
AND ia.item = ita.item (+)
AND pm.brand_id = mb.brand_id (+)
AND to_char(ia.fulfillment_method) = cd.code
AND cd.code_type = 'EIFM') v_product
,(SELECT item
,product_id
,MIN(DECODE(row_num,1,variant_id)) variant_id1
,MIN(DECODE(row_num,1,value_id)) value_id1
,MIN(DECODE(row_num,2,variant_id)) variant_id2
,MIN(DECODE(row_num,2,value_id)) value_id2
,MIN(DECODE(row_num,3,variant_id)) variant_id3
,MIN(DECODE(row_num,3,value_id)) value_id3
,MIN(DECODE(row_num,4,variant_id)) variant_id4
,MIN(DECODE(row_num,4,value_id)) value_id4
,MIN(DECODE(row_num,5,variant_id)) variant_id5
,MIN(DECODE(row_num,5,value_id)) value_id5
,MIN(DECODE(row_num,6,variant_id)) variant_id6
,MIN(DECODE(row_num,6,value_id)) value_id6
FROM (SELECT item
,product_id
,row_number () OVER (PARTITION BY item ORDER BY variant_id) row_num
,variant_id
,value_id
FROM mffecom.item_variant)
GROUP BY item
,product_id) v_variant
,item_temp it
WHERE v_item.item = v_selling.item (+)
AND v_item.item = v_product.item (+)
AND v_item.item = v_variant.item (+)
AND v_item.item = it.item
AND v_item.supplier = NVL(:PM_supplier,v_item.supplier)
AND (v_product.prod_created_by = :PM_prod_created_by OR :PM_prod_created_by IS NULL)
AND (v_product.item_created_by = :PM_item_created_by OR :PM_item_created_by IS NULL)
AND (v_product.prod_created_date BETWEEN :prod_created_date_start AND :prod_created_date_to OR :prod_created_date_start IS NULL)
AND (v_product.item_created_date BETWEEN :item_created_date_start AND :item_created_date_to OR :item_created_date_start IS NULL)
ORDER BY v_product.product_id

Expression.Error Power Query EXCEL

I have a problem with a dynamic parameter in Power Query. There's the code:
let
Parametro = Excel.CurrentWorkbook(){[Name="Parametro"]}[Content],
InicioExec_Valor = Parametro{0}[Valor],
FimExec_Valor = Parametro{1}[Valor],
Fonte = Sql.Database("DATABASE", "TABLE", [Query="select#(lf)#(lf)o.cd_controle, exe.nm_pessoa AS Executante, o.numero AS OM, #(lf)CONVERT(nvarchar(10), o.dt_abertura, 103) AS Abertura,#(lf)o.medidor Horimetro_OM,#(lf)p.nm_pessoa AS Cliente, #(lf)e.nm_equipto AS Equipamento, pat.nr_patrimonio AS Patrimonio, #(lf)CONVERT(nvarchar(10), o.dt_autoriz_execucao, 103) AS Inicio_Exec, #(lf)CONVERT(nvarchar(10), o.dt_encos_oficina, 103) AS Fim_Exec, Z.nm_apelido AS Unidade, #(lf)CASE WHEN fl_preventiva = 'C' THEN 'Corretiva' #(lf)WHEN fl_preventiva = 'P' then 'PREVENTIVA'#(lf)WHEN fl_preventiva = 'R' then 'INSPEÇÃO RESUMIDA'#(lf)WHEN fl_preventiva = 'V' then 'INSPEÇÃO PREVENTIVA'#(lf)WHEN fl_preventiva = 'E' then 'ENTREGA TÉCNICA'#(lf)else 'Indefinido' end AS 'Corret_Preven'#(lf),CONVERT(nvarchar(10), fl_remessa.dt_saida, 103) AS DataSaida#(lf),fl_rem_equ.vl_medidor Horimetro_Remessa#(lf),CONVERT(nvarchar(10), o.dt_abertura, 103) AS Abertura#(lf),o.medidor Horimetro_OM#(lf)#(lf)from orcos o#(lf)inner join controle c on (c.cd_controle = o.cd_controle)#(lf)inner join wcore_oid oid on (oid.cd_oid = c.cd_oid)#(lf)left outer join empresa AS Z ON Z.cd_empresa = o.cd_empresa #(lf)left outer join equipto e on (e.cd_equipto = o.cd_equipto)#(lf)left outer join pessoa f on (f.cd_pessoa = o.cd_pessoa_tec)#(lf)left outer join pessoa p on (p.cd_pessoa = o.cd_pessoa)#(lf)left outer join pessoa exe on (exe.cd_pessoa = o.cd_pessoa_exe)#(lf)left outer join patrimon pat on (pat.cd_patrimonio = o.cd_patrimonio)#(lf)left outer join est_almox x on x.cd_almox = pat.cd_almox#(lf)left outer join empresa emp on emp.cd_empresa = o.cd_empresa_origem #(lf)#(lf)left outer JOIN dbo.fich_loc ON (fich_loc.cd_controle= o.cd_controle_loc)#(lf)left outer JOIN dbo.fl_remessa fl_remessa ON (fl_remessa.cd_controle = dbo.fich_loc.cd_controle)#(lf)#(lf)INNER JOIN(select max(fl_remessa.cd_flremessa)cd_flremessa, A.cd_controle #(lf)#(tab)#(tab)#(tab)#(tab)#(tab) from dbo.fl_remessa#(lf)#(tab)#(tab)#(tab)#(tab)#(tab) inner join dbsislocsalvador..fich_loc on (fl_remessa.cd_controle = dbo.fich_loc.cd_controle)#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) inner join dbsislocsalvador..orcos a on (fich_loc.cd_controle= A.cd_controle_loc#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and fl_remessa.dt_saida<=a.dt_abertura) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) inner join controle c on (c.cd_controle = A.cd_controle)#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) left outer join patrimon pat on (pat.cd_patrimonio = a.cd_patrimonio)#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) LEFT OUTER JOIN dbo.fl_rem_equ AS fl_rem_equ ON pat.cd_patrimonio = fl_rem_equ.cd_patrimonio AND fl_remessa.cd_flremessa = fl_rem_equ.cd_flremessa#(lf) #(tab)#(tab) left outer JOIN dbo.loc_flremequ_xplano AS loc_flremequ_xplano ON loc_flremequ_xplano.cd_flremequ = fl_rem_equ.cd_flremequ #(lf) #(tab)#(tab)#(tab) left outer JOIN dbo.equipto AS equipto ON fl_rem_equ.cd_equipto = equipto.cd_equipto#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) where #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) ((NOT EXISTS(select top 1 * from config_tag_xoid)) OR (c.cd_oid not in (select txo.cd_oid from #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) (select * from ( select cd_tag, nm_tag , (30) as fl_acesso from config_tag t where t.fl_ativo in ('S') ) tags #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) WHERE (fl_acesso = 10) ) tags inner join config_tag_xoid txo on tags.cd_tag = txo.cd_tag where 3=3 /*filter_tag_clause*/ #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) group by txo.cd_oid))) and ( ( (a.cd_controle_loc is not null and a.cd_fldevolucao is null) ) ) and#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) (a.cd_empresa IN (24,45,5,46,20,29,43,15,48,10,1,22,8,34,49,9,47,52,7)) and ( ( a.cd_local is null or a.cd_local in (0,1,2,3,5,6,7,8,9) ) ) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) AND (a.cd_controle_loc IS NOT NULL) AND (a.cd_fldevolucao IS NULL) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and (a.fl_preventiva in ('C', 'P', 'R', 'V', 'E')) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and (equipto.cd_grupo in (1478, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1488, 1491, 1492, 1548, 1549))#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) group by a.cd_controle) AS fl_remessa_max #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) ON fl_remessa.cd_flremessa = fl_remessa_max.cd_flremessa#(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) and fl_remessa_max.cd_controle = o.cd_controle #(lf)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)LEFT OUTER JOIN dbo.fl_rem_equ AS fl_rem_equ ON pat.cd_patrimonio = fl_rem_equ.cd_patrimonio AND fl_remessa.cd_flremessa = fl_rem_equ.cd_flremessa#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab)#(tab) #(lf)left outer JOIN dbo.equipto AS equipto ON fl_rem_equ.cd_equipto = equipto.cd_equipto#(lf) #(tab)#(tab)#(tab)#(tab)#(tab) #(lf)WHERE ((NOT EXISTS(select top 1 * from config_tag_xoid)) OR (c.cd_oid not in (select txo.cd_oid from #(lf)(select * from ( select cd_tag, nm_tag , (30) as fl_acesso from config_tag t where t.fl_ativo in ('S') ) tags #(lf)WHERE (fl_acesso = 10) ) tags inner join config_tag_xoid txo on tags.cd_tag = txo.cd_tag where 3=3 /*filter_tag_clause*/ #(lf)group by txo.cd_oid))) and ( ( (o.cd_controle_loc is not null and o.cd_fldevolucao is null) ) ) and#(lf)(o.cd_empresa IN (24,45,5,46,20,29,43,15,48,10,1,22,8,34,49,9,47,52,7)) and ( ( o.cd_local is null or o.cd_local in (0,1,2,3,5,6,7,8,9) ) ) #(lf)AND (o.cd_controle_loc IS NOT NULL) AND (o.cd_fldevolucao IS NULL) #(lf)and (o.fl_preventiva in ('C', 'P', 'R', 'V', 'E')) #(lf)and (equipto.cd_grupo in (1478, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1488, 1491, 1492, 1548, 1549))"]),
#"Tipo Alterado" = Table.TransformColumnTypes(Fonte,{{"Abertura", type date}, {"Inicio_Exec", type date}, {"Fim_Exec", type date}, {"DataSaida", type date}}),
#"Colunas Removidas" = Table.RemoveColumns(#"Tipo Alterado",{"cd_controle", "Horimetro_OM2", "Abertura2"}),
#"Filtro Datas" = Table.SelectRows(#"Colunas Removidas", each [Fim_Exec] >= InicioExec_Valor and [Fim_Exec] <= FimExec_Valor)
in
#"Filtro Datas"
And occur that error:
Expression.Error: Não conseguimos aplicar o operador < aos tipos
Number e Date. Detalhes:
Operator=<
Left=42795
Right=01/06/2007
How can I solve that?
Note: MY PARAMETER (01/03/2017) ARE FORMATED AS TEXT.
Although I don't read Spanish, looks like it says you cannot compare number and date with < operator.
It is good practice, by the way, to first convert parameters to the proper type:
InicioExec_Valor = Date.From(Parametro{0}[Valor]),
FimExec_Valor = Date.From(Parametro{1}[Valor]),`
Try this.
If it won't work, determine step that generates error by clicking them one-by-one.
There is more I wonder about. Why do you convert Fim_Exec to nvarchar and then to date?
1. #(lf)CONVERT(nvarchar(10), o.dt_encos_oficina, 103) AS Fim_Exec
2. {"Fim_Exec", type date},
Why don't use Fim_Exec = CAST(o.dt_encos_oficina as date)? (or datetime, depending on SQL Server version) in the query?
Same applies to other columns.
Next, it is best practice not to use native queries unless absolutely required.
And, yes, this is largest and most complex query I've seen up to date. :)

Faster Query Execution

I have 42 columns and 4 joins in my table. Execution needs 140 seconds, I want to reduce the time of execution. What to do?
SELECT DISTINCT GR_ID,
CONVERT(VARCHAR, GR_DT, 106) AS GRDate,
GR_GN,
GR_GID,
GR_BS,
GR_BN,
GR_DC,
GR_COP,
GR_PA,
GR_AR,
GR_BG,
GR_SM,
GR_KG,
GR_IN,
GR_TS,
CMLC_BC,
CMLC_SC,
CMLC_KG,
CMLC_BL,
CMBC_BC,
CMBC_SC,
CMBC_KG,
CMBC_BL,
GR_BRN,
GR_BLC,
GR_LEA,
GR_ALF,
GR_BTA,
GR_PID,
GR_UTS,
GR_LR,
CMGB_BC,
CMGB_SC,
CMGB_KG,
CMGB_BL,
CMGB_LR,
GR_UPC,
GR_UPT,
GR_UTA,
GR_TTA,
GR_SPA,
GR_SAR,
GR_NR,
ISNULL(ACK_ACK, 'NO') AS ACK_ACK
FROM GR
LEFT JOIN CMBC
ON ( CMBC_ID = GR_GID )
AND ( CMBC_IID = GR_PID )
RIGHT JOIN CMLC
ON ( CMLC_ID = GR_GID )
AND ( CMLC_IID = GR_PID )
RIGHT JOIN CMGB
ON ( CMGB_ID = GR_GID )
AND ( CMGB_IID = GR_PID )
AND ( CMGB_TS = GR_UTS )
LEFT JOIN ACK
ON ( ACK_GN = GR_GN )
AND ( ACK_GID = GR_GID )
ORDER BY GR_GN,
GR_GID

SQL: not exist with openquery not working as expected

I do have an oracle 8 database from which I want to fetch data to SQL Server 2005. The following statement works fine, if the table on SQL Server 2005 is empty. If I run with let's say one entry missing, it does not work. Please let me know, if any additional information would be usefull!
SELECT wdmsoracle.NO
FROM (Select * from OPENQUERY(msch, 'select
actu.NO
, actu.ANNOSEQUENT
, replace(replace(dm.descript, ''Generated by modification request '', ''Modification request''), ''Pseudo ECR for WDMS-SAP interface'', ''New Drawing'')
, actu.APPRDATE
, actu.MADEDATE
, actu.MAINDRW
, actu.DESIGNGRP
, actu.ITEMID
, actu.ISSUE
, actu.DESCRIPT
, actu.DESCRIPT2
, dr.PRODUCTCODE
, mpaths.webpath as asdasd
, vpaths.webpath
FROM
WDDRAWACTU actu
, wddraw dr
, wddvfilesiss vfiles
, wddvpaths vpaths
, wddmfiles mfiles
, wddmpaths mpaths
, wdanno dm
WHERE actu.apprdate >= to_date(''01-01-05'',''dd-mm-yy'')
and actu.itemid = dr.itemid (+)
and actu.issue = dr.issue (+)
and actu.annosequent = dm.sequent (+)
and actu.itemid = vfiles.itemid (+)
and actu.issue = vfiles.issue (+)
and vfiles.pathid = vpaths.pathid
and actu.annosequent = mfiles.sequent (+)
and mfiles.pathid = mpaths.pathid')) as wdmsoracle
Where NOT EXISTS (
SELECT wdmsoracle.NO
FROM [DesignMessage_Workflow].[dbo].[WDMS] wdms, (Select * from OPENQUERY(msch, 'select
actu.NO
FROM
WDDRAWACTU actu
WHERE actu.apprdate >= to_date(''01-01-05'',''dd-mm-yy'')')) as wdmsoracle
WHERE wdms.NO = wdmsoracle.NO)
with the help of this website I was able to solve it on my own:
SELECT wdmsoracle.NO
FROM (Select * from OPENQUERY(msch, 'select
actu.NO
, actu.ANNOSEQUENT
, replace(replace(dm.descript, ''Generated by modification request '', ''Modification request''), ''Pseudo ECR for WDMS-SAP interface'', ''New Drawing'')
, actu.APPRDATE
, actu.MADEDATE
, actu.MAINDRW
, actu.DESIGNGRP
, actu.ITEMID
, actu.ISSUE
, actu.DESCRIPT
, actu.DESCRIPT2
, dr.PRODUCTCODE
, mpaths.webpath as asdasd
, vpaths.webpath
FROM
WDDRAWACTU actu
, wddraw dr
, wddvfilesiss vfiles
, wddvpaths vpaths
, wddmfiles mfiles
, wddmpaths mpaths
, wdanno dm
WHERE actu.apprdate >= to_date(''01-01-05'',''dd-mm-yy'')
and actu.itemid = dr.itemid (+)
and actu.issue = dr.issue (+)
and actu.annosequent = dm.sequent (+)
and actu.itemid = vfiles.itemid (+)
and actu.issue = vfiles.issue (+)
and vfiles.pathid = vpaths.pathid
and actu.annosequent = mfiles.sequent (+)
and mfiles.pathid = mpaths.pathid')) as wdmsoracle
Where NOT EXISTS (
SELECT wdms.NO
FROM [DesignMessage_Workflow].[dbo].[WDMS] wdms
WHERE wdms.NO = wdmsoracle.NO)