Error message which I can't explain/solve in sql - sql

This code gives this error which I really don't get (the Select works ok):
'SQL Error [2022] [42601]: SQL compilation error: Missing column
specification'
CREATE OR REPLACE TABLE ANALYSE_PMN.DTE_INSTALL_WFM AS
(
SELECT BUT000.PARTNER AS ZPARTNER ,
BUT000.BPKIND AS SRT ,
FKKVKP.KTOKL AS REKENINGKLASSE ,
EVER.VKONTO AS CONTRACTREKENING ,
EVER.VERTRAG AS CONTRACT ,
TO_CHAR(EVER.EINZDAT, 'YYYYMMDD') AS INHUISDATUM ,
TO_CHAR(EVER.AUSZDAT, 'YYYYMMDD') AS UITHUISDATUM ,
EANL.ANLAGE AS AANSLUITING ,
EANL.SPARTE AS PRODUCT ,
EANLH.ABLEINH AS PORTIE ,
TO_CHAR(EANLH.AB, 'YYYYMMDD') AS VANAF ,
TO_CHAR(EANLH.BIS, 'YYYYMMDD') AS TOT ,
SUBSTR(EANLH.ABLEINH, 2, 4) AS PORTIE_MMDD ,
DTE_PERIODE.PERIODE AS GEPL_AFR_YYYYMM ,
DTE_PERIODE.PERIODE
|| SUBSTR(EANLH.ABLEINH, 4, 2) AS GEPL_AFR_YYYYMMDD ,
NVL(ZZEAN_VERBR_HG, 0) ,
NVL(ZZEAN_VERBR_LG, 0) ,
NVL(ZZEAN_VERBR_HG, 0) + NVL(ZZEAN_VERBR_LG, 0) AS ZZEAN_VERBR ,
NVL(ZZEAN_TERUG_HG, 0) ,
NVL(ZZEAN_TERUG_LG, 0) ,
NVL(ZZEAN_TERUG_HG, 0) + NVL(ZZEAN_TERUG_LG, 0) AS ZZEAN_TERUG ,
(NVL(ZZEAN_VERBR_HG, 0) + NVL(ZZEAN_VERBR_LG, 0)) - (NVL(ZZEAN_TERUG_HG, 0) + NVL(ZZEAN_TERUG_LG, 0)) AS ZZEAN_VERBR_TOTAAL
FROM ANALYSE_PMN.DTE_PERIODE ,
OWNER_OSP.OSP_EVER EVER
JOIN OWNER_OSP.OSP_EANL EANL
ON EANL.ANLAGE = EVER.ANLAGE
JOIN OWNER_OSP.OSP_EANLH EANLH
ON EANLH.ANLAGE = EVER.ANLAGE
JOIN OWNER_OSP.OSP_FKKVKP FKKVKP
ON EVER.VKONTO = FKKVKP.VKONT
JOIN OWNER_OSP.OSP_BUT000 BUT000
ON BUT000.PARTNER = FKKVKP.GPART
WHERE EANL.SPARTE IN ( 'E' ,
'G' )
AND TO_CHAR(EVER.EINZDAT, 'YYYYMMDD') < SUBSTR(DTE_PERIODE.PERIODE, 1, 6)
|| SUBSTR(EANLH.ABLEINH, 4, 2)
AND EVER.AUSZDAT = '9999-12-31'
AND TO_DATE(DTE_PERIODE.PERIODE
|| SUBSTR(EANLH.ABLEINH, 4, 2), 'YYYYMMDD') BETWEEN EANLH.AB AND EANLH.BIS
AND EANL.BEGRU = 'R'
AND EANLH.ABLEINH LIKE 'C%'
AND SUBSTR(DTE_PERIODE.PERIODE, 5, 2) = SUBSTR(EANLH.ABLEINH, 2, 2)
) ;

Some of your columns do not have AS clauses. Can you try this:
CREATE OR REPLACE TABLE ANALYSE_PMN.DTE_INSTALL_WFM AS
(
SELECT BUT000.PARTNER AS ZPARTNER ,
BUT000.BPKIND AS SRT ,
FKKVKP.KTOKL AS REKENINGKLASSE ,
EVER.VKONTO AS CONTRACTREKENING ,
EVER.VERTRAG AS CONTRACT ,
TO_CHAR(EVER.EINZDAT, 'YYYYMMDD') AS INHUISDATUM ,
TO_CHAR(EVER.AUSZDAT, 'YYYYMMDD') AS UITHUISDATUM ,
EANL.ANLAGE AS AANSLUITING ,
EANL.SPARTE AS PRODUCT ,
EANLH.ABLEINH AS PORTIE ,
TO_CHAR(EANLH.AB, 'YYYYMMDD') AS VANAF ,
TO_CHAR(EANLH.BIS, 'YYYYMMDD') AS TOT ,
SUBSTR(EANLH.ABLEINH, 2, 4) AS PORTIE_MMDD ,
DTE_PERIODE.PERIODE AS GEPL_AFR_YYYYMM ,
DTE_PERIODE.PERIODE|| SUBSTR(EANLH.ABLEINH, 4, 2) AS GEPL_AFR_YYYYMMDD ,
NVL(ZZEAN_VERBR_HG, 0) AS ZZEAN_VERBR_HG,
NVL(ZZEAN_VERBR_LG, 0) AS ZZEAN_VERBR_LG,
NVL(ZZEAN_VERBR_HG, 0) + NVL(ZZEAN_VERBR_LG, 0) AS ZZEAN_VERBR ,
NVL(ZZEAN_TERUG_HG, 0) AS ZZEAN_TERUG_HG,
NVL(ZZEAN_TERUG_LG, 0) AS ZZEAN_TERUG_LG,
NVL(ZZEAN_TERUG_HG, 0) + NVL(ZZEAN_TERUG_LG, 0) AS ZZEAN_TERUG ,
(NVL(ZZEAN_VERBR_HG, 0) + NVL(ZZEAN_VERBR_LG, 0)) - (NVL(ZZEAN_TERUG_HG, 0) + NVL(ZZEAN_TERUG_LG, 0)) AS ZZEAN_VERBR_TOTAAL
FROM ANALYSE_PMN.DTE_PERIODE ,
OWNER_OSP.OSP_EVER EVER
JOIN OWNER_OSP.OSP_EANL EANL
ON EANL.ANLAGE = EVER.ANLAGE
JOIN OWNER_OSP.OSP_EANLH EANLH
ON EANLH.ANLAGE = EVER.ANLAGE
JOIN OWNER_OSP.OSP_FKKVKP FKKVKP
ON EVER.VKONTO = FKKVKP.VKONT
JOIN OWNER_OSP.OSP_BUT000 BUT000
ON BUT000.PARTNER = FKKVKP.GPART
WHERE EANL.SPARTE IN ( 'E' ,
'G' )
AND TO_CHAR(EVER.EINZDAT, 'YYYYMMDD') < SUBSTR(DTE_PERIODE.PERIODE, 1, 6)
|| SUBSTR(EANLH.ABLEINH, 4, 2)
AND EVER.AUSZDAT = '9999-12-31'
AND TO_DATE(DTE_PERIODE.PERIODE
|| SUBSTR(EANLH.ABLEINH, 4, 2), 'YYYYMMDD') BETWEEN EANLH.AB AND EANLH.BIS
AND EANL.BEGRU = 'R'
AND EANLH.ABLEINH LIKE 'C%'
AND SUBSTR(DTE_PERIODE.PERIODE, 5, 2) = SUBSTR(EANLH.ABLEINH, 2, 2)
) ;

Related

How to get per hour in data set?

I'm trying to get data per hour however as I execute, the data for day is displayed in the table. IDC_YMD with per hour result must be in the table and I'm not sure if I have the right SUBSTR(string, start, length).
SELECT EQP_ID, EQP_NAME, 'WorkPreparation' GUBUN,WorkPreparation
CNT,SITE_CODE,GODS_CODE,STEP_NAME,IDC_YMD,IDC_SUMR_UNIT_CODE,MTBI,MTBA
FROM
(
SELECT CASE WHEN WorkPreparation = 0 THEN '-' ELSE EQP_ID END AS EQP_ID, CASE WHEN WorkPreparation =
0 THEN '-' ELSE SUBSTR(EQP_NAME,1,4) END AS EQP_NAME,
WorkPreparation,SITE_CODE,GODS_CODE,STEP_NAME,IDC_YMD,IDC_SUMR_UNIT_CODE,MTBI,MTBA
FROM(
SELECT EQP_ID, EVENT_COMMENT EQP_NAME, SUM(WORK_PRE_CNT) WorkPreparation, A.SITE_CODE
, A.GODS_CODE
, A.EES_STEP_ID AS STEP_NAME
, TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12, 2))) || '/' || TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12,
2))) AS IDC_YMD
, 'D' AS IDC_SUMR_UNIT_CODE
, CASE WHEN (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0))) = 0
THEN ROUND(SUM(NVL(A.OPER_MOP, 0)),1)
ELSE ROUND(SUM(NVL(A.OPER_MOP, 0)) / (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0))) /
60,1)
END AS MTBI
, CASE WHEN (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0)) + SUM(NVL(A.WORK_PRE_CNT, 0)) +
SUM(NVL(A.PROD_CHANGE_CNT, 0)) + SUM(NVL(A.QUAL_EXATN_CNT, 0)) +
SUM(NVL(A.MTR_EXATN_CNT, 0)) + SUM(NVL(A.STEP_CHRC_LOSS_CNT, 0)) +
SUM(NVL(A.IDLE_3MN_UND_EXCPT_CNT, 0))) = 0
THEN 0
ELSE ROUND(SUM(NVL(A.OPER_MOP, 0)) / (SUM(NVL(A.INTR_CNT, 0)) + SUM(NVL(A.BRDN_CNT, 0)) +
SUM(NVL(A.WORK_PRE_CNT, 0)) + SUM(NVL(A.PROD_CHANGE_CNT, 0)) + SUM(NVL(A.QUAL_EXATN_CNT, 0)) +
SUM(NVL(A.MTR_EXATN_CNT, 0)) + SUM(NVL(A.STEP_CHRC_LOSS_CNT,
0)) + SUM(NVL(A.IDLE_3MN_UND_EXCPT_CNT, 0))) / 60,1)
END AS MTBA
FROM IFR_EES_PCL A
WHERE SITE_CODE = 'E502AA'
AND GODS_CODE = 'N5210'
AND MTBI_MNG_YN = 'Y'
AND EES_STEP_ID = 'TP'
AND A.IDC_YMD BETWEEN TO_CHAR(SYSDATE-6, 'YYYYMMDDHH') AND TO_CHAR(SYSDATE, 'YYYYMMDD')
GROUP BY EQP_ID, EVENT_COMMENT,A.SITE_CODE
, A.GODS_CODE
,A.EQP_TYPE
, A.EES_STEP_ID
, TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12, 2)))
, A.IDC_YMD
ORDER BY A.IDC_YMD, MTBI DESC--TO_CHAR(TO_NUMBER(SUBSTR(A.IDC_YMD, 12, 2)))
))
WHERE ROWNUM <= 10
and EQP_NAME like 'TA%'
Thank you!

need help optimizing SQL Server query

I am working on our Reporting database. We've been having alot of trouble with our current data dumps process, and I'm trying to rework it.
Every hour, we append data into our tables from production. Talking about one table specifically, it has just over a million rows (usually less). I need to grab those rows and append it into our reporting server table. Currently, I'm using a stored procedure that uses the MERGE process (with target, source technique). It takes about 16min each hour for that to run. Anyone have a better idea to append/update rows? The production table that we hit also only contains TODAY's data, so its not like I even have to put in a date filter to reduce data load. What could be the best way to optimize performance?
Here is the execution plan:https://www.brentozar.com/pastetheplan/?id=HJrfFsTXP
Below is the procedure:
CREATE PROCEDURE dbo.spINSERT_FULL_ORDERHEADER
AS
BEGIN
WITH ORDER_DETAIL_V2 AS(
SELECT
'DCMS' AS SOURCE,
DC_ORDER_NUMBER,
DCMS_ORDER_TYPE,
CUSTOMER_PURCHASE_ORDER_ID,
BILL_TO,
CUSTOMER_MASTER_RECORD_TYPE,
SHIP_TO,
SALES_ORDER,
MERCHANDISE_STYLE,
MERCHANDISE_SIZE_1,
CONCAT(SUBSTRING(MERCHANDISE_STYLE,3,6),'-',
SUBSTRING(MERCHANDISE_STYLE,9,3)) AS MATERIAL,
SUBSTRING(MERCHANDISE_STYLE,14,2) AS QUALITY,
ORDER_QTY,
ORDER_SELECTED_QTY,
ORDER_SHIPPED_QTY,
RELEASED_QTY,
PICK_QTY,
PACKED_QTY,
RELEASED_CARTONS,
FULL_CASE_PICK_COUNT,
TOTE_COUNT,
SPECIAL_PROCESS_CODE_1,
SPECIAL_PROCESS_CODE_2,
SPECIAL_PROCESS_CODE_3,
ORDER_DETAIL_STATUS,
ORDER_CARRIER,
CARRIER_SERVICE_ID,
CAST(CONVERT(VARCHAR, CREATE_DATE) AS DATE) AS CREATE_DATE,
LEFT(cast(dateadd(hh,2,LEFT(RIGHT(CONCAT('000000', CREATE_TIME),6), 2)+':'+SUBSTRING(RIGHT(CONCAT('000000', CREATE_TIME), 6), 3, 2)+':'+RIGHT(RIGHT(CONCAT('000000', CREATE_TIME), 8), 2)) AS TIME),8) AS CREATE_TIME,
CAST(CONVERT(VARCHAR, ALLOCATION_DATE) AS DATE) AS ALLOCATION_DATE,
CAST(CONVERT(VARCHAR, REQUESTED_SHIP_DATE) AS DATE) AS REQUESTED_SHIP_DATE,
CAST(CONVERT(VARCHAR, CANCEL_DATE) AS DATE) AS CANCEL_DATE,
CASE
WHEN DISPATCH_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, DISPATCH_DATE) AS DATE)
END AS DISPATCH_DATE,
CASE
WHEN RELEASED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, RELEASED_DATE) AS DATE)
END AS RELEASED_DATE,
LEFT(RIGHT(CONCAT('000000', RELEASED_TIME), 6), 2)+':'+SUBSTRING(RIGHT(CONCAT('000000',
RELEASED_TIME), 6), 3, 2) +':'+ RIGHT(RIGHT(CONCAT('000000', RELEASED_TIME), 6), 2) AS RELEASED_TIME,
PRIORITY_ORDER,
PARTIAL_SHIP,
SHIPMENT_CARTONS,
SCANNED_CARTONS,
STAGED_CARTONS,
LOADED_CARTONS,
CASE
WHEN INVOICE_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, INVOICE_DATE) AS DATE)
END AS INVOICE_DATE,
SHIPPING_LOAD_NUMBER,
STARTING_WAVE,
ENDING_WAVE,
CASE
WHEN STAGED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, STAGED_DATE) AS DATE)
END AS STAGED_DATE,
ORDER_HDR_STATUS,
LEFT(HOST_ORDER_NUMBER, 9) AS DELIVERY_NUMBER,
CASE
WHEN NOT_SELECTED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, NOT_SELECTED_DATE) AS DATE)
END AS NOT_SELECTED_DATE,
LEFT(RIGHT(CONCAT('000000', NOT_SELECTED_TIME), 6), 2)+':'+SUBSTRING(RIGHT(CONCAT('000000', NOT_SELECTED_TIME), 6), 3, 2)+':'+RIGHT(RIGHT(CONCAT('000000', NOT_SELECTED_TIME), 6), 2) AS NOT_SELECTED_TIME,
NOT_SELECTED_REASON,
CAST(CONVERT(VARCHAR, EST_DEL_DT) AS DATE) AS EST_DEL_DATE,
CONCAT(DC_ORDER_NUMBER,MERCHANDISE_STYLE,MERCHANDISE_SIZE_1,CREATE_DATE) AS [KEY]
FROM OPENQUERY(MEMDWPR1,'
SELECT
OHORNO AS DC_ORDER_NUMBER,
OHORTY AS DCMS_ORDER_TYPE,
OHCUPO AS CUSTOMER_PURCHASE_ORDER_ID,
OHBLTO AS BILL_TO,
OHCURT AS CUSTOMER_MASTER_RECORD_TYPE,
OHSHTO AS SHIP_TO,
OHSAOR AS SALES_ORDER,
ODMRSY AS MERCHANDISE_STYLE,
ODMRS1 AS MERCHANDISE_SIZE_1,
ODORQT AS ORDER_QTY,
ODORSL AS ORDER_SELECTED_QTY,
ODORSH AS ORDER_SHIPPED_QTY,
OHRLQT AS RELEASED_QTY,
OHPKQT AS PICK_QTY,
OHPDQT AS PACKED_QTY,
OHRLCA AS RELEASED_CARTONS,
OHFCCT AS FULL_CASE_PICK_COUNT,
OHTOCT AS TOTE_COUNT,
ODSP01 AS SPECIAL_PROCESS_CODE_1,
ODSP02 AS SPECIAL_PROCESS_CODE_2,
ODSP03 AS SPECIAL_PROCESS_CODE_3,
ODODST AS ORDER_DETAIL_STATUS,
OHORCR AS ORDER_CARRIER,
OHCRSV AS CARRIER_SERVICE_ID,
OHCRDT AS CREATE_DATE,
OHCRTM AS CREATE_TIME,
OHALDT AS ALLOCATION_DATE,
OHRQDT AS REQUESTED_SHIP_DATE,
OHCNDT AS CANCEL_DATE,
OHDIDT AS DISPATCH_DATE,
OHRLDT AS RELEASED_DATE,
OHRLTM AS RELEASED_TIME,
OHORPY AS PRIORITY_ORDER,
OHPLSH AS PARTIAL_SHIP,
OHSHCA AS SHIPMENT_CARTONS,
OHSNCA AS SCANNED_CARTONS,
OHSGCA AS STAGED_CARTONS,
OHLDCA AS LOADED_CARTONS,
OHIVDT AS INVOICE_DATE,
OHLDNO AS SHIPPING_LOAD_NUMBER,
OHWVST AS STARTING_WAVE,
OHWVED AS ENDING_WAVE,
OHSGDT AS STAGED_DATE,
OHOHST AS ORDER_HDR_STATUS,
OHHSOR AS HOST_ORDER_NUMBER,
OHSLDT AS NOT_SELECTED_DATE,
OHSLTM AS NOT_SELECTED_TIME,
OHSLRS AS NOT_SELECTED_REASON,
OHEXDD AS EST_DEL_DT
FROM DSV052BDTA.OHORDR00#MEMWMS2.WORLD
JOIN DSV052BDTA.ODORDR00#MEMWMS2.WORLD
ON ODORNO = OHORNO
')
GROUP BY
DC_ORDER_NUMBER,
DCMS_ORDER_TYPE,
CUSTOMER_PURCHASE_ORDER_ID,
BILL_TO,
CUSTOMER_MASTER_RECORD_TYPE,
SHIP_TO,
SALES_ORDER,
MERCHANDISE_STYLE,
MERCHANDISE_SIZE_1,
CONCAT(SUBSTRING(MERCHANDISE_STYLE,3,6),'-',
SUBSTRING(MERCHANDISE_STYLE,9,3)),
SUBSTRING(MERCHANDISE_STYLE,14,2),
ORDER_QTY,
ORDER_SELECTED_QTY,
ORDER_SHIPPED_QTY,
RELEASED_QTY,
PICK_QTY,
PACKED_QTY,
RELEASED_CARTONS,
FULL_CASE_PICK_COUNT,
TOTE_COUNT,
SPECIAL_PROCESS_CODE_1,
SPECIAL_PROCESS_CODE_2,
SPECIAL_PROCESS_CODE_3,
ORDER_DETAIL_STATUS,
ORDER_CARRIER,
CARRIER_SERVICE_ID,
CAST(CONVERT(VARCHAR, CREATE_DATE) AS DATE),
LEFT(cast(dateadd(hh,2,LEFT(RIGHT(CONCAT('000000', CREATE_TIME),6), 2)+':'+SUBSTRING(RIGHT(CONCAT('000000', CREATE_TIME), 6), 3, 2)+':'+RIGHT(RIGHT(CONCAT('000000', CREATE_TIME), 8), 2)) AS TIME),8),
CAST(CONVERT(VARCHAR, ALLOCATION_DATE) AS DATE) ,
CAST(CONVERT(VARCHAR, REQUESTED_SHIP_DATE) AS DATE) ,
CAST(CONVERT(VARCHAR, CANCEL_DATE) AS DATE) ,
CASE
WHEN DISPATCH_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, DISPATCH_DATE) AS DATE)
END,
CASE
WHEN RELEASED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, RELEASED_DATE) AS DATE)
END,
LEFT(RIGHT(CONCAT('000000', RELEASED_TIME), 6), 2)+':'+SUBSTRING(RIGHT(CONCAT('000000',
RELEASED_TIME), 6), 3, 2) +':'+ RIGHT(RIGHT(CONCAT('000000', RELEASED_TIME), 6), 2) ,
PRIORITY_ORDER,
PARTIAL_SHIP,
SHIPMENT_CARTONS,
SCANNED_CARTONS,
STAGED_CARTONS,
LOADED_CARTONS,
CASE
WHEN INVOICE_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, INVOICE_DATE) AS DATE)
END,
SHIPPING_LOAD_NUMBER,
STARTING_WAVE,
ENDING_WAVE,
CASE
WHEN STAGED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, STAGED_DATE) AS DATE)
END,
ORDER_HDR_STATUS,
LEFT(HOST_ORDER_NUMBER, 9),
CASE
WHEN NOT_SELECTED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, NOT_SELECTED_DATE) AS DATE)
END,
LEFT(RIGHT(CONCAT('000000', NOT_SELECTED_TIME), 6), 2)+':'+SUBSTRING(RIGHT(CONCAT('000000', NOT_SELECTED_TIME), 6), 3, 2)+':'+RIGHT(RIGHT(CONCAT('000000', NOT_SELECTED_TIME), 6), 2),
NOT_SELECTED_REASON,
CAST(CONVERT(VARCHAR, EST_DEL_DT) AS DATE),
CONCAT(DC_ORDER_NUMBER,MERCHANDISE_STYLE,MERCHANDISE_SIZE_1,CREATE_DATE)
)
MERGE BUSINESSPLANNING_TEST.dbo.[FullOrderHeader] AS TARGET
USING ORDER_DETAIL_V2 AS SOURCE
ON TARGET.[KEY] = SOURCE.[KEY]
WHEN MATCHED THEN
UPDATE SET
TARGET.[SOURCE] = SOURCE.[SOURCE],
TARGET.DC_ORDER_NUMBER = SOURCE.DC_ORDER_NUMBER,
TARGET.DCMS_ORDER_TYPE = SOURCE.DCMS_ORDER_TYPE,
TARGET.CUSTOMER_PURCHASE_ORDER_ID = SOURCE.CUSTOMER_PURCHASE_ORDER_ID,
TARGET.BILL_TO = SOURCE.BILL_TO,
TARGET.CUSTOMER_MASTER_RECORD_TYPE = SOURCE.CUSTOMER_MASTER_RECORD_TYPE,
TARGET.SHIP_TO = SOURCE.SHIP_TO,
TARGET.SALES_ORDER = SOURCE.SALES_ORDER,
TARGET.MERCHANDISE_STYLE = SOURCE.MERCHANDISE_STYLE,
TARGET.MERCHANDISE_SIZE_1 = SOURCE.MERCHANDISE_SIZE_1,
TARGET.MATERIAL = SOURCE.MATERIAL,
TARGET.QUALITY = SOURCE.QUALITY,
TARGET.ORDER_QTY = SOURCE.ORDER_QTY,
TARGET.ORDER_SELECTED_QTY = SOURCE.ORDER_SELECTED_QTY,
TARGET.ORDER_SHIPPED_QTY = SOURCE.ORDER_SHIPPED_QTY,
TARGET.RELEASED_QTY = SOURCE.RELEASED_QTY,
TARGET.PICK_QTY = SOURCE.PICK_QTY,
TARGET.PACKED_QTY = SOURCE.PACKED_QTY,
TARGET.RELEASED_CARTONS = SOURCE.RELEASED_CARTONS,
TARGET.FULL_CASE_PICK_COUNT = SOURCE.FULL_CASE_PICK_COUNT,
TARGET.TOTE_COUNT = SOURCE.TOTE_COUNT,
TARGET.SPECIAL_PROCESS_CODE_1 = SOURCE.SPECIAL_PROCESS_CODE_1,
TARGET.SPECIAL_PROCESS_CODE_2 = SOURCE.SPECIAL_PROCESS_CODE_2,
TARGET.SPECIAL_PROCESS_CODE_3 = SOURCE.SPECIAL_PROCESS_CODE_3,
TARGET.ORDER_DETAIL_STATUS = SOURCE.ORDER_DETAIL_STATUS,
TARGET.ORDER_CARRIER = SOURCE.ORDER_CARRIER,
TARGET.CARRIER_SERVICE_ID = SOURCE.CARRIER_SERVICE_ID,
TARGET.CREATE_DATE = SOURCE.CREATE_DATE,
TARGET.CREATE_TIME = SOURCE.CREATE_TIME,
TARGET.ALLOCATION_DATE = SOURCE.ALLOCATION_DATE,
TARGET.REQUESTED_SHIP_DATE = SOURCE.REQUESTED_SHIP_DATE,
TARGET.CANCEL_DATE = SOURCE.CANCEL_DATE,
TARGET.DISPATCH_DATE = SOURCE.DISPATCH_DATE,
TARGET.RELEASED_DATE = SOURCE.RELEASED_DATE,
TARGET.RELEASED_TIME = SOURCE.RELEASED_TIME,
TARGET.PRIORITY_ORDER = SOURCE.PRIORITY_ORDER,
TARGET.PARTIAL_SHIP = SOURCE.PARTIAL_SHIP,
TARGET.SHIPMENT_CARTONS = SOURCE.SHIPMENT_CARTONS,
TARGET.SCANNED_CARTONS = SOURCE.SCANNED_CARTONS,
TARGET.STAGED_CARTONS = SOURCE.STAGED_CARTONS,
TARGET.LOADED_CARTONS = SOURCE.LOADED_CARTONS,
TARGET.INVOICE_DATE = SOURCE.INVOICE_DATE,
TARGET.SHIPPING_LOAD_NUMBER = SOURCE.SHIPPING_LOAD_NUMBER,
TARGET.STARTING_WAVE = SOURCE.STARTING_WAVE,
TARGET.ENDING_WAVE = SOURCE.ENDING_WAVE,
TARGET.STAGED_DATE = SOURCE.STAGED_DATE,
TARGET.ORDER_HDR_STATUS = SOURCE.ORDER_HDR_STATUS,
TARGET.DELIVERY_NUMBER = SOURCE.DELIVERY_NUMBER,
TARGET.NOT_SELECTED_DATE = SOURCE.NOT_SELECTED_DATE,
TARGET.NOT_SELECTED_TIME = SOURCE.NOT_SELECTED_TIME,
TARGET.NOT_SELECTED_REASON = SOURCE.NOT_SELECTED_REASON,
TARGET.EST_DEL_DATE = SOURCE.EST_DEL_DATE,
TARGET.LAST_UPDATED = GETDATE(),
TARGET.[KEY] = SOURCE.[KEY]
WHEN NOT MATCHED BY TARGET
THEN INSERT
(
[SOURCE],
DC_ORDER_NUMBER,
DCMS_ORDER_TYPE,
CUSTOMER_PURCHASE_ORDER_ID,
BILL_TO,
CUSTOMER_MASTER_RECORD_TYPE,
SHIP_TO,
SALES_ORDER,
MERCHANDISE_STYLE,
MERCHANDISE_SIZE_1,
MATERIAL,
QUALITY,
ORDER_QTY,
ORDER_SELECTED_QTY,
ORDER_SHIPPED_QTY,
RELEASED_QTY,
PICK_QTY,
PACKED_QTY,
RELEASED_CARTONS,
FULL_CASE_PICK_COUNT,
TOTE_COUNT,
SPECIAL_PROCESS_CODE_1,
SPECIAL_PROCESS_CODE_2,
SPECIAL_PROCESS_CODE_3,
ORDER_DETAIL_STATUS,
ORDER_CARRIER,
CARRIER_SERVICE_ID,
CREATE_DATE,
CREATE_TIME,
ALLOCATION_DATE,
REQUESTED_SHIP_DATE,
CANCEL_DATE,
DISPATCH_DATE,
RELEASED_DATE,
RELEASED_TIME,
PRIORITY_ORDER,
PARTIAL_SHIP,
SHIPMENT_CARTONS,
SCANNED_CARTONS,
STAGED_CARTONS,
LOADED_CARTONS,
INVOICE_DATE,
SHIPPING_LOAD_NUMBER,
STARTING_WAVE,
ENDING_WAVE,
STAGED_DATE,
ORDER_HDR_STATUS,
DELIVERY_NUMBER,
NOT_SELECTED_DATE,
NOT_SELECTED_TIME,
NOT_SELECTED_REASON,
EST_DEL_DATE,
LAST_UPDATED,
[KEY]
)
VALUES
(
SOURCE.SOURCE,
SOURCE.DC_ORDER_NUMBER,
SOURCE.DCMS_ORDER_TYPE,
SOURCE.CUSTOMER_PURCHASE_ORDER_ID,
SOURCE.BILL_TO,
SOURCE.CUSTOMER_MASTER_RECORD_TYPE,
SOURCE.SHIP_TO,
SOURCE.SALES_ORDER,
SOURCE.MERCHANDISE_STYLE,
SOURCE.MERCHANDISE_SIZE_1,
SOURCE.MATERIAL,
SOURCE.QUALITY,
SOURCE.ORDER_QTY,
SOURCE.ORDER_SELECTED_QTY,
SOURCE.ORDER_SHIPPED_QTY,
SOURCE.RELEASED_QTY,
SOURCE.PICK_QTY,
SOURCE.PACKED_QTY,
SOURCE.RELEASED_CARTONS,
SOURCE.FULL_CASE_PICK_COUNT,
SOURCE.TOTE_COUNT,
SOURCE.SPECIAL_PROCESS_CODE_1,
SOURCE.SPECIAL_PROCESS_CODE_2,
SOURCE.SPECIAL_PROCESS_CODE_3,
SOURCE.ORDER_DETAIL_STATUS,
SOURCE.ORDER_CARRIER,
SOURCE.CARRIER_SERVICE_ID,
SOURCE.CREATE_DATE,
SOURCE.CREATE_TIME,
SOURCE.ALLOCATION_DATE,
SOURCE.REQUESTED_SHIP_DATE,
SOURCE.CANCEL_DATE,
SOURCE.DISPATCH_DATE,
SOURCE.RELEASED_DATE,
SOURCE.RELEASED_TIME,
SOURCE.PRIORITY_ORDER,
SOURCE.PARTIAL_SHIP,
SOURCE.SHIPMENT_CARTONS,
SOURCE.SCANNED_CARTONS,
SOURCE.STAGED_CARTONS,
SOURCE.LOADED_CARTONS,
SOURCE.INVOICE_DATE,
SOURCE.SHIPPING_LOAD_NUMBER,
SOURCE.STARTING_WAVE,
SOURCE.ENDING_WAVE,
SOURCE.STAGED_DATE,
SOURCE.ORDER_HDR_STATUS,
SOURCE.DELIVERY_NUMBER,
SOURCE.NOT_SELECTED_DATE,
SOURCE.NOT_SELECTED_TIME,
SOURCE.NOT_SELECTED_REASON,
SOURCE.EST_DEL_DATE,
GETDATE(),
SOURCE.[KEY]
);
END
First of all sorry for not using a comment but my reputation is not high enough so I'm going to answer this way.
You query is pretty simple in terms of tables involved, so there is not much to try (maybe thinking out of the box but I don't have any idea now).
According to the plan it shows that it's using a hash join, and the estimated amount of rows for target and source are 1M and 10K respectively (it's not clear to me from your description). It also shows that most of the estimated cost is due to the scan in the target table and to the hash (71+15 %). So, depending on the amount of rows in the target and source and if you have an index on dbo.FullOrderHeader.Key, it might be possible that forcing a loop join could work better (assuming that the estimates are accurate it should). The higher the amount of rows in the source, the less convenient the loop will be.
You can do that by adding OPTION (LOOP JOIN) at the end of the MERGE statement before the ;.
If the amount of source
CREATE PROCEDURE dbo.spINSERT_FULL_ORDERHEADER
AS
BEGIN
WITH ORDER_DETAIL_V2
AS (
SELECT 'DCMS' AS SOURCE
,DC_ORDER_NUMBER
,DCMS_ORDER_TYPE
,CUSTOMER_PURCHASE_ORDER_ID
,BILL_TO
,CUSTOMER_MASTER_RECORD_TYPE
,SHIP_TO
,SALES_ORDER
,MERCHANDISE_STYLE
,MERCHANDISE_SIZE_1
,CONCAT (
SUBSTRING(MERCHANDISE_STYLE, 3, 6)
,'-'
,SUBSTRING(MERCHANDISE_STYLE, 9, 3)
) AS MATERIAL
,SUBSTRING(MERCHANDISE_STYLE, 14, 2) AS QUALITY
,ORDER_QTY
,ORDER_SELECTED_QTY
,ORDER_SHIPPED_QTY
,RELEASED_QTY
,PICK_QTY
,PACKED_QTY
,RELEASED_CARTONS
,FULL_CASE_PICK_COUNT
,TOTE_COUNT
,SPECIAL_PROCESS_CODE_1
,SPECIAL_PROCESS_CODE_2
,SPECIAL_PROCESS_CODE_3
,ORDER_DETAIL_STATUS
,ORDER_CARRIER
,CARRIER_SERVICE_ID
,CAST(CONVERT(VARCHAR, CREATE_DATE) AS DATE) AS CREATE_DATE
,LEFT(cast(dateadd(hh, 2, LEFT(RIGHT(CONCAT (
'000000'
,CREATE_TIME
), 6), 2) + ':' + SUBSTRING(RIGHT(CONCAT (
'000000'
,CREATE_TIME
), 6), 3, 2) + ':' + RIGHT(RIGHT(CONCAT (
'000000'
,CREATE_TIME
), 8), 2)) AS TIME), 8) AS CREATE_TIME
,CAST(CONVERT(VARCHAR, ALLOCATION_DATE) AS DATE) AS ALLOCATION_DATE
,CAST(CONVERT(VARCHAR, REQUESTED_SHIP_DATE) AS DATE) AS REQUESTED_SHIP_DATE
,CAST(CONVERT(VARCHAR, CANCEL_DATE) AS DATE) AS CANCEL_DATE
,CASE
WHEN DISPATCH_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, DISPATCH_DATE) AS DATE)
END AS DISPATCH_DATE
,CASE
WHEN RELEASED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, RELEASED_DATE) AS DATE)
END AS RELEASED_DATE
,LEFT(RIGHT(CONCAT (
'000000'
,RELEASED_TIME
), 6), 2) + ':' + SUBSTRING(RIGHT(CONCAT (
'000000'
,RELEASED_TIME
), 6), 3, 2) + ':' + RIGHT(RIGHT(CONCAT (
'000000'
,RELEASED_TIME
), 6), 2) AS RELEASED_TIME
,PRIORITY_ORDER
,PARTIAL_SHIP
,SHIPMENT_CARTONS
,SCANNED_CARTONS
,STAGED_CARTONS
,LOADED_CARTONS
,CASE
WHEN INVOICE_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, INVOICE_DATE) AS DATE)
END AS INVOICE_DATE
,SHIPPING_LOAD_NUMBER
,STARTING_WAVE
,ENDING_WAVE
,CASE
WHEN STAGED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, STAGED_DATE) AS DATE)
END AS STAGED_DATE
,ORDER_HDR_STATUS
,LEFT(HOST_ORDER_NUMBER, 9) AS DELIVERY_NUMBER
,CASE
WHEN NOT_SELECTED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, NOT_SELECTED_DATE) AS DATE)
END AS NOT_SELECTED_DATE
,LEFT(RIGHT(CONCAT (
'000000'
,NOT_SELECTED_TIME
), 6), 2) + ':' + SUBSTRING(RIGHT(CONCAT (
'000000'
,NOT_SELECTED_TIME
), 6), 3, 2) + ':' + RIGHT(RIGHT(CONCAT (
'000000'
,NOT_SELECTED_TIME
), 6), 2) AS NOT_SELECTED_TIME
,NOT_SELECTED_REASON
,CAST(CONVERT(VARCHAR, EST_DEL_DT) AS DATE) AS EST_DEL_DATE
,CONCAT (
DC_ORDER_NUMBER
,MERCHANDISE_STYLE
,MERCHANDISE_SIZE_1
,CREATE_DATE
) AS [KEY]
FROM OPENQUERY(MEMDWPR1,
'
SELECT
OHORNO AS DC_ORDER_NUMBER,
OHORTY AS DCMS_ORDER_TYPE,
OHCUPO AS CUSTOMER_PURCHASE_ORDER_ID,
OHBLTO AS BILL_TO,
OHCURT AS CUSTOMER_MASTER_RECORD_TYPE,
OHSHTO AS SHIP_TO,
OHSAOR AS SALES_ORDER,
ODMRSY AS MERCHANDISE_STYLE,
ODMRS1 AS MERCHANDISE_SIZE_1,
ODORQT AS ORDER_QTY,
ODORSL AS ORDER_SELECTED_QTY,
ODORSH AS ORDER_SHIPPED_QTY,
OHRLQT AS RELEASED_QTY,
OHPKQT AS PICK_QTY,
OHPDQT AS PACKED_QTY,
OHRLCA AS RELEASED_CARTONS,
OHFCCT AS FULL_CASE_PICK_COUNT,
OHTOCT AS TOTE_COUNT,
ODSP01 AS SPECIAL_PROCESS_CODE_1,
ODSP02 AS SPECIAL_PROCESS_CODE_2,
ODSP03 AS SPECIAL_PROCESS_CODE_3,
ODODST AS ORDER_DETAIL_STATUS,
OHORCR AS ORDER_CARRIER,
OHCRSV AS CARRIER_SERVICE_ID,
OHCRDT AS CREATE_DATE,
OHCRTM AS CREATE_TIME,
OHALDT AS ALLOCATION_DATE,
OHRQDT AS REQUESTED_SHIP_DATE,
OHCNDT AS CANCEL_DATE,
OHDIDT AS DISPATCH_DATE,
OHRLDT AS RELEASED_DATE,
OHRLTM AS RELEASED_TIME,
OHORPY AS PRIORITY_ORDER,
OHPLSH AS PARTIAL_SHIP,
OHSHCA AS SHIPMENT_CARTONS,
OHSNCA AS SCANNED_CARTONS,
OHSGCA AS STAGED_CARTONS,
OHLDCA AS LOADED_CARTONS,
OHIVDT AS INVOICE_DATE,
OHLDNO AS SHIPPING_LOAD_NUMBER,
OHWVST AS STARTING_WAVE,
OHWVED AS ENDING_WAVE,
OHSGDT AS STAGED_DATE,
OHOHST AS ORDER_HDR_STATUS,
OHHSOR AS HOST_ORDER_NUMBER,
OHSLDT AS NOT_SELECTED_DATE,
OHSLTM AS NOT_SELECTED_TIME,
OHSLRS AS NOT_SELECTED_REASON,
OHEXDD AS EST_DEL_DT
FROM DSV052BDTA.OHORDR00#MEMWMS2.WORLD
JOIN DSV052BDTA.ODORDR00#MEMWMS2.WORLD
ON ODORNO = OHORNO
'
)
GROUP BY DC_ORDER_NUMBER
,DCMS_ORDER_TYPE
,CUSTOMER_PURCHASE_ORDER_ID
,BILL_TO
,CUSTOMER_MASTER_RECORD_TYPE
,SHIP_TO
,SALES_ORDER
,MERCHANDISE_STYLE
,MERCHANDISE_SIZE_1
,CONCAT (
SUBSTRING(MERCHANDISE_STYLE, 3, 6)
,'-'
,SUBSTRING(MERCHANDISE_STYLE, 9, 3)
)
,SUBSTRING(MERCHANDISE_STYLE, 14, 2)
,ORDER_QTY
,ORDER_SELECTED_QTY
,ORDER_SHIPPED_QTY
,RELEASED_QTY
,PICK_QTY
,PACKED_QTY
,RELEASED_CARTONS
,FULL_CASE_PICK_COUNT
,TOTE_COUNT
,SPECIAL_PROCESS_CODE_1
,SPECIAL_PROCESS_CODE_2
,SPECIAL_PROCESS_CODE_3
,ORDER_DETAIL_STATUS
,ORDER_CARRIER
,CARRIER_SERVICE_ID
,CAST(CONVERT(VARCHAR, CREATE_DATE) AS DATE)
,LEFT(cast(dateadd(hh, 2, LEFT(RIGHT(CONCAT (
'000000'
,CREATE_TIME
), 6), 2) + ':' + SUBSTRING(RIGHT(CONCAT (
'000000'
,CREATE_TIME
), 6), 3, 2) + ':' + RIGHT(RIGHT(CONCAT (
'000000'
,CREATE_TIME
), 8), 2)) AS TIME), 8)
,CAST(CONVERT(VARCHAR, ALLOCATION_DATE) AS DATE)
,CAST(CONVERT(VARCHAR, REQUESTED_SHIP_DATE) AS DATE)
,CAST(CONVERT(VARCHAR, CANCEL_DATE) AS DATE)
,CASE
WHEN DISPATCH_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, DISPATCH_DATE) AS DATE)
END
,CASE
WHEN RELEASED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, RELEASED_DATE) AS DATE)
END
,LEFT(RIGHT(CONCAT (
'000000'
,RELEASED_TIME
), 6), 2) + ':' + SUBSTRING(RIGHT(CONCAT (
'000000'
,RELEASED_TIME
), 6), 3, 2) + ':' + RIGHT(RIGHT(CONCAT (
'000000'
,RELEASED_TIME
), 6), 2)
,PRIORITY_ORDER
,PARTIAL_SHIP
,SHIPMENT_CARTONS
,SCANNED_CARTONS
,STAGED_CARTONS
,LOADED_CARTONS
,CASE
WHEN INVOICE_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, INVOICE_DATE) AS DATE)
END
,SHIPPING_LOAD_NUMBER
,STARTING_WAVE
,ENDING_WAVE
,CASE
WHEN STAGED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, STAGED_DATE) AS DATE)
END
,ORDER_HDR_STATUS
,LEFT(HOST_ORDER_NUMBER, 9)
,CASE
WHEN NOT_SELECTED_DATE = 0
THEN NULL
ELSE CAST(CONVERT(VARCHAR, NOT_SELECTED_DATE) AS DATE)
END
,LEFT(RIGHT(CONCAT (
'000000'
,NOT_SELECTED_TIME
), 6), 2) + ':' + SUBSTRING(RIGHT(CONCAT (
'000000'
,NOT_SELECTED_TIME
), 6), 3, 2) + ':' + RIGHT(RIGHT(CONCAT (
'000000'
,NOT_SELECTED_TIME
), 6), 2)
,NOT_SELECTED_REASON
,CAST(CONVERT(VARCHAR, EST_DEL_DT) AS DATE)
,CONCAT (
DC_ORDER_NUMBER
,MERCHANDISE_STYLE
,MERCHANDISE_SIZE_1
,CREATE_DATE
)
)
MERGE BUSINESSPLANNING_TEST.dbo.[FullOrderHeader] AS TARGET
USING ORDER_DETAIL_V2 AS SOURCE
ON TARGET.[KEY] = SOURCE.[KEY]
WHEN MATCHED
THEN
UPDATE
SET TARGET.[SOURCE] = SOURCE.[SOURCE]
,TARGET.DC_ORDER_NUMBER = SOURCE.DC_ORDER_NUMBER
,TARGET.DCMS_ORDER_TYPE = SOURCE.DCMS_ORDER_TYPE
,TARGET.CUSTOMER_PURCHASE_ORDER_ID = SOURCE.CUSTOMER_PURCHASE_ORDER_ID
,TARGET.BILL_TO = SOURCE.BILL_TO
,TARGET.CUSTOMER_MASTER_RECORD_TYPE = SOURCE.CUSTOMER_MASTER_RECORD_TYPE
,TARGET.SHIP_TO = SOURCE.SHIP_TO
,TARGET.SALES_ORDER = SOURCE.SALES_ORDER
,TARGET.MERCHANDISE_STYLE = SOURCE.MERCHANDISE_STYLE
,TARGET.MERCHANDISE_SIZE_1 = SOURCE.MERCHANDISE_SIZE_1
,TARGET.MATERIAL = SOURCE.MATERIAL
,TARGET.QUALITY = SOURCE.QUALITY
,TARGET.ORDER_QTY = SOURCE.ORDER_QTY
,TARGET.ORDER_SELECTED_QTY = SOURCE.ORDER_SELECTED_QTY
,TARGET.ORDER_SHIPPED_QTY = SOURCE.ORDER_SHIPPED_QTY
,TARGET.RELEASED_QTY = SOURCE.RELEASED_QTY
,TARGET.PICK_QTY = SOURCE.PICK_QTY
,TARGET.PACKED_QTY = SOURCE.PACKED_QTY
,TARGET.RELEASED_CARTONS = SOURCE.RELEASED_CARTONS
,TARGET.FULL_CASE_PICK_COUNT = SOURCE.FULL_CASE_PICK_COUNT
,TARGET.TOTE_COUNT = SOURCE.TOTE_COUNT
,TARGET.SPECIAL_PROCESS_CODE_1 = SOURCE.SPECIAL_PROCESS_CODE_1
,TARGET.SPECIAL_PROCESS_CODE_2 = SOURCE.SPECIAL_PROCESS_CODE_2
,TARGET.SPECIAL_PROCESS_CODE_3 = SOURCE.SPECIAL_PROCESS_CODE_3
,TARGET.ORDER_DETAIL_STATUS = SOURCE.ORDER_DETAIL_STATUS
,TARGET.ORDER_CARRIER = SOURCE.ORDER_CARRIER
,TARGET.CARRIER_SERVICE_ID = SOURCE.CARRIER_SERVICE_ID
,TARGET.CREATE_DATE = SOURCE.CREATE_DATE
,TARGET.CREATE_TIME = SOURCE.CREATE_TIME
,TARGET.ALLOCATION_DATE = SOURCE.ALLOCATION_DATE
,TARGET.REQUESTED_SHIP_DATE = SOURCE.REQUESTED_SHIP_DATE
,TARGET.CANCEL_DATE = SOURCE.CANCEL_DATE
,TARGET.DISPATCH_DATE = SOURCE.DISPATCH_DATE
,TARGET.RELEASED_DATE = SOURCE.RELEASED_DATE
,TARGET.RELEASED_TIME = SOURCE.RELEASED_TIME
,TARGET.PRIORITY_ORDER = SOURCE.PRIORITY_ORDER
,TARGET.PARTIAL_SHIP = SOURCE.PARTIAL_SHIP
,TARGET.SHIPMENT_CARTONS = SOURCE.SHIPMENT_CARTONS
,TARGET.SCANNED_CARTONS = SOURCE.SCANNED_CARTONS
,TARGET.STAGED_CARTONS = SOURCE.STAGED_CARTONS
,TARGET.LOADED_CARTONS = SOURCE.LOADED_CARTONS
,TARGET.INVOICE_DATE = SOURCE.INVOICE_DATE
,TARGET.SHIPPING_LOAD_NUMBER = SOURCE.SHIPPING_LOAD_NUMBER
,TARGET.STARTING_WAVE = SOURCE.STARTING_WAVE
,TARGET.ENDING_WAVE = SOURCE.ENDING_WAVE
,TARGET.STAGED_DATE = SOURCE.STAGED_DATE
,TARGET.ORDER_HDR_STATUS = SOURCE.ORDER_HDR_STATUS
,TARGET.DELIVERY_NUMBER = SOURCE.DELIVERY_NUMBER
,TARGET.NOT_SELECTED_DATE = SOURCE.NOT_SELECTED_DATE
,TARGET.NOT_SELECTED_TIME = SOURCE.NOT_SELECTED_TIME
,TARGET.NOT_SELECTED_REASON = SOURCE.NOT_SELECTED_REASON
,TARGET.EST_DEL_DATE = SOURCE.EST_DEL_DATE
,TARGET.LAST_UPDATED = GETDATE()
,TARGET.[KEY] = SOURCE.[KEY]
WHEN NOT MATCHED BY TARGET
THEN
INSERT (
[SOURCE]
,DC_ORDER_NUMBER
,DCMS_ORDER_TYPE
,CUSTOMER_PURCHASE_ORDER_ID
,BILL_TO
,CUSTOMER_MASTER_RECORD_TYPE
,SHIP_TO
,SALES_ORDER
,MERCHANDISE_STYLE
,MERCHANDISE_SIZE_1
,MATERIAL
,QUALITY
,ORDER_QTY
,ORDER_SELECTED_QTY
,ORDER_SHIPPED_QTY
,RELEASED_QTY
,PICK_QTY
,PACKED_QTY
,RELEASED_CARTONS
,FULL_CASE_PICK_COUNT
,TOTE_COUNT
,SPECIAL_PROCESS_CODE_1
,SPECIAL_PROCESS_CODE_2
,SPECIAL_PROCESS_CODE_3
,ORDER_DETAIL_STATUS
,ORDER_CARRIER
,CARRIER_SERVICE_ID
,CREATE_DATE
,CREATE_TIME
,ALLOCATION_DATE
,REQUESTED_SHIP_DATE
,CANCEL_DATE
,DISPATCH_DATE
,RELEASED_DATE
,RELEASED_TIME
,PRIORITY_ORDER
,PARTIAL_SHIP
,SHIPMENT_CARTONS
,SCANNED_CARTONS
,STAGED_CARTONS
,LOADED_CARTONS
,INVOICE_DATE
,SHIPPING_LOAD_NUMBER
,STARTING_WAVE
,ENDING_WAVE
,STAGED_DATE
,ORDER_HDR_STATUS
,DELIVERY_NUMBER
,NOT_SELECTED_DATE
,NOT_SELECTED_TIME
,NOT_SELECTED_REASON
,EST_DEL_DATE
,LAST_UPDATED
,[KEY]
)
VALUES (
SOURCE.SOURCE
,SOURCE.DC_ORDER_NUMBER
,SOURCE.DCMS_ORDER_TYPE
,SOURCE.CUSTOMER_PURCHASE_ORDER_ID
,SOURCE.BILL_TO
,SOURCE.CUSTOMER_MASTER_RECORD_TYPE
,SOURCE.SHIP_TO
,SOURCE.SALES_ORDER
,SOURCE.MERCHANDISE_STYLE
,SOURCE.MERCHANDISE_SIZE_1
,SOURCE.MATERIAL
,SOURCE.QUALITY
,SOURCE.ORDER_QTY
,SOURCE.ORDER_SELECTED_QTY
,SOURCE.ORDER_SHIPPED_QTY
,SOURCE.RELEASED_QTY
,SOURCE.PICK_QTY
,SOURCE.PACKED_QTY
,SOURCE.RELEASED_CARTONS
,SOURCE.FULL_CASE_PICK_COUNT
,SOURCE.TOTE_COUNT
,SOURCE.SPECIAL_PROCESS_CODE_1
,SOURCE.SPECIAL_PROCESS_CODE_2
,SOURCE.SPECIAL_PROCESS_CODE_3
,SOURCE.ORDER_DETAIL_STATUS
,SOURCE.ORDER_CARRIER
,SOURCE.CARRIER_SERVICE_ID
,SOURCE.CREATE_DATE
,SOURCE.CREATE_TIME
,SOURCE.ALLOCATION_DATE
,SOURCE.REQUESTED_SHIP_DATE
,SOURCE.CANCEL_DATE
,SOURCE.DISPATCH_DATE
,SOURCE.RELEASED_DATE
,SOURCE.RELEASED_TIME
,SOURCE.PRIORITY_ORDER
,SOURCE.PARTIAL_SHIP
,SOURCE.SHIPMENT_CARTONS
,SOURCE.SCANNED_CARTONS
,SOURCE.STAGED_CARTONS
,SOURCE.LOADED_CARTONS
,SOURCE.INVOICE_DATE
,SOURCE.SHIPPING_LOAD_NUMBER
,SOURCE.STARTING_WAVE
,SOURCE.ENDING_WAVE
,SOURCE.STAGED_DATE
,SOURCE.ORDER_HDR_STATUS
,SOURCE.DELIVERY_NUMBER
,SOURCE.NOT_SELECTED_DATE
,SOURCE.NOT_SELECTED_TIME
,SOURCE.NOT_SELECTED_REASON
,SOURCE.EST_DEL_DATE
,GETDATE()
,SOURCE.[KEY]
) OPTION (LOOP JOIN);
END

Error (Conversion failed when converting date and/or time from character string) while executing the view

When I execute this view I get this error:-
Conversion failed when converting date and/or time from character string
Please help me out what kind of mistake I'm making in this given query??*
SQL Query:
WITH CTE_aacd1 AS (
SELECT *
FROM (
SELECT LEAD_CO_MNE
,ACCOUNT_NO
,CLOSURE_TYPE
,DATE_TIME
,Rank() OVER (ORDER BY [DATE_TIME]) AS rank_
FROM [InsightSource].BS.AA_ACCOUNT_CLOSURE_DETAILS
) aat
WHERE aat.rank_ = 1
)
SELECT [Date].BusinessDate as [BusinessDate] , aa.LEAD_CO_MNE as LeadCompany , 'BS' as SourceSystem , concat('BS:' , com.FINANCIAL_MNE , ':' , cast(aa.[LINKED_APPL_ID] as nvarchar(50))) as [SourceAccountId] , concat('BS:' ,CO_CHK.COMPANY_MNE , ':' , CAST(isnull(A.[CO_CODE], aa.[CO_CODE]) as nvarchar(50))) as [SourceBranchId] , concat('BS:' , COM.CUSTOMER_MNEMONIC , ':' , cast(isnull(A.[CUSTOMER], aa.[CUSTOMER]) as nvarchar(50))) as [SourceCustomerId] , concat('BS:' , CO_CHK.COMPANY_MNE , ':' , cast(A.[ACCOUNT_OFFICER] as nvarchar(50))) as [SourceEmployeeId] , concat('BS:' , com.CUSTOMER_MNEMONIC , ':' , l.[#id]) as [SourceLimitId] --,A.[CATEGORY] as [sourceGLId] ,aa.[LINKED_APPL_ID] as [AccountNum]
,-1 * Rb.Debitbal as Balance ,-1 * rb.ForeignDebitBal as [ForeignCurrencyBal] ,'Loan' as [Category] ,A.[CURRENCY] as [Currency] ,-1 * rb.InterestAccrued as [InterestAccrued] ,rb.InterestRate as InterestRate ,rb.FixOrVAr ,rb.Rate_tier_type as RateType --,rb.InterestRateIndex ,rb.InterestRateVariance ,p.[#ID] as [ProductCode] ,P.DESCRIPTION as [ProductDesc] ,aa.PRODUCT_GROUP as [ProductType] ,aa.PRODUCT_GROUP as [T24ProductGroup] ,aa.[START_DATE] as [StartDate] ,isnull(aa.ORIG_CONTRACT_DATE, aa.[START_DATE]) OriginalStartDate ,case when a.INACTIV_MARKER = 'Y' then 'InActive' else AA.[ARR_STATUS] end as [StatusCode] ,a.[ACCOUNT_TITLE_1] as [StatementDesc] ,isnull(aacd.RENEWAL_DATE, aacd.MATURITY_DATE) as MaturityDate ,cast(aacd.MATURITY_DATE as date) as AmortMatureDate ,cast(aata.[AMOUNT] as decimal (28,4)) as OriginalLoanAmount ,cast(aata.[AMOUNT] as decimal (28,4)) as Authorized ,right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) as TermUnit ,try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 )) as Term ,case when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'M' then try_convert(int,SUBSTRING(Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 )) when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'D' then try_convert(int,round(try_convert(int,SUBSTRING(Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))*(1/(365.25/12)) , 0)) when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'Y' then try_convert(int,round(try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))*12 , 0))
else NULL end as TermInMonths, case when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'M' then try_convert(int,round(try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))*30.44,0)) when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'D' then try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 )) when right(rtrim(Isnull(aacp.CHANGE_PERIOD, aata.TERM)), 1) = 'Y' then try_convert(int,round(try_convert(int,SUBSTRING( Isnull(aacp.CHANGE_PERIOD, aata.TERM), 1, len(Isnull(aacp.CHANGE_PERIOD, aata.TERM))-1 ))*365.25 , 0)) else NULL end as TermInDays ,aacd.[START_DATE] as DisburseDate ,case when aacd.ARR_AGE_STATUS = 'DEL' then 'Yes' else 'No' end as IsDelinquent ,del.DelinquentAmount ,del.LastDelinquentDate -- (!!! ERROR !!!),case when LastDelinquentDate is not null and isdate(del.LastDelinquentDate) = 1 then datediff( d, del.LastDelinquentDate, date.BusinessDate ) end as DelinquentDays ,case when (LastDelinquentDate is not null) and (TRY_CONVERT(DATE, del.LastDelinquentDate) IS NOT NULL) then datediff(d, TRY_CONVERT(DATE, del.LastDelinquentDate), [date].BusinessDate ) end as DelinquentDays ,aacd1.CLOSURE_TYPE as ReasonClosed ,cast(left('20' + aacd1.DATE_TIME, 8) as date) as ClosedDate ,-1 * rb.DebitBal as [AvailableFunds] ,left(arrschedule.Start_date, 8) as FirstPmtDate ,arrschedule.Calc_Amount as ScheduledPmtAmt ,case when payment_freq like '%e1M%' and payment_freq not like '%o%,%D%' then 'Monthly' when payment_freq like '%e1M%' and payment_freq like '%o%,%D%' then 'Twice a month' when payment_freq like '%e2M%' and payment_freq not like '%o%,%D%' then 'Every 2 month' when payment_freq like '%e2M%' and payment_freq like '%o%,%D%' then 'Twice every 2 month' when payment_freq like '%e1W%' or payment_freq like '%e7D%' then 'Weekly' when payment_freq like '%e2W%' or payment_freq like '%e14D%' then 'Bi-Weekly' else 'N/A' end as PmtFreq ,case when intfreq like '%e1M%' and intfreq not like '%o%,%D%' then 'Monthly' when intFreq like '%e1M%' and intfreq like '%o%,%D%' then 'Twice a month' when intFreq like '%e2M%' and intfreq not like '%o%,%D%' then 'Every 2 month' when intFreq like '%e2M%' and intfreq like '%o%,%D%' then 'Twice every 2 month' when payment_freq like '%e1W%' or payment_freq like '%e7D%' then 'Weekly' when payment_freq like '%e2W%' or payment_freq like '%e14D%' then 'Bi-Weekly' else 'N/A' end as InterestPaidFreq , arrschedule.payment_type as PmtCalcMethod ,'BSAA_Lending' as SystemSource FROM [InsightSource].[BS].[AA_ARRANGEMENT] aa LEFT JOIN [InsightSource].[BS].[COMPANY] COM ON COM.MNEMONIC = AA.BRANCH_CO_MNE LEFT JOIN [InsightSource].[BS].[COMPANY_CHECK_Company_Mne] CO_CHK ON CO_CHK.[#ID] = 'MASTER' and CO_CHK.[Sequence] = 1 LEFT JOIN [InsightSource].[BS].[ACCOUNT] a ON a.LEAD_CO_MNE = COM.FINANCIAL_MNE AND A.[#ID] = aa.LINKED_APPL_ID LEFT JOIN [InsightSource].BS.LIMIT l ON a.CUSTOMER +'.' + RIGHT ( '0000' + a.LIMIT_REF , 10) = l.[#ID] AND l.LEAD_CO_MNE = COM.CUSTOMER_MNEMONIC LEFT JOIN (Select del1.[#id], DEL1.ARRANGEMENT_ID, Del1.LEAD_CO_MNE, del1.LastDelinquentDate, SUM(cast(bd.OS_TOTAL_AMOUNT as decimal (28,4))) as DelinquentAmount from
(select aad.[#id], AAD.LEAD_CO_MNE, AAD.ARRANGEMENT_ID, min(billpay.Bill_Pay_date) as LastDelinquentDate, max(billpay.Bill_Pay_date) as MaxDelinquentDate
from
( select [#id], [LEAD_CO_MNE], ARR_AGE_STATUS, [#ID] AS ARRANGEMENT_ID, BILL_PAY_DATE from [InsightSource].BS.AA_ACCOUNT_DETAILS where ARR_AGE_STATUS in ('NAB', 'DEL')) aad join
( select * from [InsightSource].BS.AA_ACCOUNT_DETAILS_Bill_Pay_date where Aging_Status
= 'DEL' or bill_status = 'Aging') billpay on aad.[#id] = billpay.[#id] and aad.[LEAD_CO_MNE] = billpay.[LEAD_CO_MNE]
group by aad.[#id], AAD.ARRANGEMENT_ID, aad.[LEAD_CO_MNE]
) Del1
join
[InsightSource].BS.AA_BILL_DETAILS bd on del1.LEAD_CO_MNE = bd.LEAD_CO_MNE AND bd.[ARRANGEMENT_ID] = DEL1.ARRANGEMENT_ID --and del1.MaxDelinquentDate = bd.actual_pay_date Group by del1.[#id], DEL1.ARRANGEMENT_ID, del1.[LEAD_CO_MNE], del1.LastDelinquentDate )del ON AA.LEAD_CO_MNE = DEL.LEAD_CO_MNE AND AA.[#ID] = DEL.ARRANGEMENT_ID LEFT JOIN (select LEAD_CO_MNE, ID_COMP_1, replace (max(CHANGE_PERIOD) , 'R_BIRTH + ' , '') as CHANGE_PERIOD from [InsightSource].BS.AA_ARR_CHANGE_PRODUCT arr1 where ID_COMP_3 = (select MAX (ID_COMP_3) from [InsightSource].BS.AA_ARR_CHANGE_PRODUCT arr2 where arr1.ID_COMP_1 = arr2.ID_COMP_1 ) group by ID_COMP_1, LEAD_CO_MNE ) aacp ON COM.FINANCIAL_MNE = aacp.LEAD_CO_MNE AND AA.[#ID] = aacp.ID_Comp_1 LEFT JOIN CTE_aacd1 aacd1 ON aacd1.LEAD_CO_MNE = COM.CUSTOMER_MNEMONIC AND aacd1.ACCOUNT_NO = A.[#ID] LEFT JOIN ( SELECT * FROM ( SELECT [#ID]
,[LEAD_CO_MNE]
,PRODUCT
,PRODUCT_STATUS
,Rank() OVER (partition by [#id]
ORDER BY [PROD_EFF_DATE]
) AS rank_
FROM [InsightSource].BS.AA_ARRANGEMENT_PRODUCT
where [PRODUCT_STATUS] = 'CURRENT'
) aap1 WHERE aap1.rank_ = 1 ) aap ON aa.[#ID] = aap.[#ID] AND AAP.[LEAD_CO_MNE] = COM.[FINANCIAL_MNE]
LEFT JOIN [InsightSource].BS.AA_PRODUCT p ON CASE WHEN COM.SPCL_FIN_FILE IS NULL THEN COM.DEFAULT_FINAN_MNE ELSE COM.SPCL_FIN_MNE END = P.BRANCH_CO_MNE AND AAP.PRODUCT = P.[#ID] LEFT JOIN [InsightSource].BS.AA_ACCOUNT_DETAILS aacd ON aacd.[LEAD_CO_MNE] = COM.FINANCIAL_MNE AND aacd.[#ID] = aa.[#ID] LEFT JOIN ( select ps.[#id] ,ps.LEAD_CO_MNE, ps.ID_Comp_1 , ps.Base_date, sfi.Calc_Amount, sf.Actual_amt, sf.payment_freq , sf.payment_type , ps.Start_date, SFI.DUE_FREQ as intFreq ,case when sf.payment_freq like '%e1M%' and sf.payment_freq not like '%o%,%D%' then 1
when sf.payment_freq like '%e1M%' and sf.payment_freq like '%o%,%D%' then 2
when sf.payment_freq like '%e2M%' and sf.payment_freq like '%o%,%D%' then 1
when sf.payment_freq like '%e1W%' then 4
when sf.payment_freq like '%e2W%' then 2
end as NumPmtInOneMonth from [InsightSource].bs.AA_ARR_PAYMENT_SCHEDULE ps join [InsightSource].bs.AA_ARR_PAYMENT_SCHEDULE_payment_freq sf on
ps.[#id] = sf.[#id] and ps.[LEAD_CO_MNE] = sf.[LEAD_CO_MNE] and sequence = 1 left join [InsightSource].bs.AA_ARR_PAYMENT_SCHEDULE_payment_freq_property sfi on ps.[#id] = sfi.[#id] and ps.[lead_co_mne] = sfi.[lead_co_mne] and sfi.property = 'INTEREST' and sfi.MVsequence = 1 and sfi.sequence = 1 where ps.id_comp_3 = (select MAX (id_comp_3) from [InsightSource].BS.AA_ARR_PAYMENT_SCHEDULE arr2 where ps.ID_COMP_1 = arr2.ID_COMP_1 ) and sfi.Calc_Amount is not null group by ps.LEAD_CO_MNE,ps.[#id] , ps.ID_Comp_1, ps.Base_date, sfi.Calc_Amount, sf.Actual_amt, sf.payment_freq , sf.payment_type , ps.Start_date ,SFI.DUE_FREQ, [bill_produced] ) arrSchedule ON aa.LEAD_CO_MNE = arrSchedule.LEAD_CO_MNE AND AA.[#ID] = arrSchedule.ID_Comp_1 LEFT JOIN (select LEAD_CO_MNE,max(REVOLVING) as REVOLVING, max(TERM) as TERM, max(AMOUNT) as amount, ID_COMP_1 from [InsightSource].BS.AA_ARR_TERM_AMOUNT arr1 where ID_COMP_3 = (select MAX (ID_COMP_3) from [InsightSource].BS.AA_ARR_TERM_AMOUNT arr2 where arr1.ID_COMP_1 = arr2.ID_COMP_1 ) group by ID_COMP_1,LEAD_CO_MNE ) aata ON COM.FINANCIAL_MNE = aata.LEAD_CO_MNE AND AA.[#ID] = aata.ID_Comp_1 LEFT JOIN [v_sourceAccountBSAA_RatesandBalances] rb ON rb.[Linked_appl_id] = aa.LINKED_APPL_ID and rb.Lead_co_mne = aa.LEAD_CO_MNE CROSS JOIN [InsightStaging].[dbo].[v_sourceDate] as [Date] WHERE aa.PRODUCT_LINE = 'LENDING' and aa.[LINKED_APPL_ID] is not null

Oracle error not a single-group group function, when I am grouping

Why does the following query give me the following error if I am including the group by:
ORA-00937: not a single-group group function
select petf.element_name en,
--tm.i,
--sum(xxpay_util.safe_to_number(prrv.result_value)) s
nvl(max(decode(tm.i, 1, sum(xxpay_util.safe_to_number(prrv.result_value)), null)), 0) e1,
nvl(max(decode(tm.i, 2, sum(xxpay_util.safe_to_number(prrv.result_value)), null)), 0) e2
from (
select rownum i,
sub.*
from (
select trunc(decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, ptp.regular_payment_date), 'MM') month_date,
min(ptp.start_date) real_month_start,
max(ptp.end_date) real_month_end
from per_business_groups pbg,
per_time_periods ptp
where pbg.business_group_id = :P_BG_ID
and ptp.payroll_id = :PAYROLL_ID
and decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, trunc(ptp.regular_payment_date, 'MM')) between xxpay_util.get_tax_year(pbg.legislation_code, :P_TAX_YEAR, 'start')
and xxpay_util.get_tax_year(pbg.legislation_code, :P_TAX_YEAR, 'end')
group by trunc(decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, ptp.regular_payment_date), 'MM')
order by 1
) sub
) tm,
pay_element_classifications pec,
pay_element_types_f petf,
pay_input_values_f pivf,
pay_run_result_values prrv,
pay_run_results prr,
pay_assignment_actions paa,
pay_payroll_actions ppa,
per_time_periods ptp
where instr('|' || :PRIMARY_CLASS_IDS || '|', '|' || to_char(nvl(pec.parent_classification_id, -6969)) || '|') > 0
and
(
petf.classification_id = pec.classification_id
or instr('|' || :PRIMARY_CLASS_IDS || '|', '|' || to_char(nvl(petf.classification_id, -6969)) || '|') > 0
)
and tm.real_month_end between petf.effective_start_date and petf.effective_end_date
and pivf.element_type_id = petf.element_type_id
and pivf.name = 'Pay Value'
and prrv.input_value_id = pivf.input_value_id
and nvl(prrv.result_value, '0') <> '0'
and prr.run_result_id = prrv.run_result_id
and prr.status in ('P', 'PA') -- RUN_RESULT_STATUS lookup
and prr.element_type_id = petf.element_type_id
and paa.assignment_action_id = prr.assignment_action_id
and paa.action_status = 'C'
and paa.assignment_id = :ASSIGNMENT_ID
and ppa.payroll_action_id = paa.payroll_action_id
and ppa.action_status = 'C'
and ppa.action_type in ('B', 'F', 'G', 'I', 'O', 'Q', 'R', 'V', 'L') -- = 'R'
and ptp.time_period_id = ppa.time_period_id
and ptp.start_date >= tm.real_month_start
and ptp.end_date <= tm.real_month_end
group by petf.element_name
-- ,tm.i
:PRIMARY_CLASS_IDS is a string such as 105|107|112|113, listing the primary classification id's.
Fixed it by putting the whole query in another sub query:
select sub2.element_name,
max(decode(sub2.i, 11, sub2.result, null)) e11,
max(decode(sub2.i, 12, sub2.result, null)) e12
from (
select petf.element_name,
tm.i,
sum(xxpay_util.safe_to_number(prrv.result_value)) result
from (
select rownum i,
sub.*
from (
select trunc(decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, ptp.regular_payment_date), 'MM') month_date,
min(ptp.start_date) real_month_start,
max(ptp.end_date) real_month_end
from per_business_groups pbg,
per_time_periods ptp
where pbg.business_group_id = :P_BG_ID
and ptp.payroll_id = :PAYROLL_ID
and decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, trunc(ptp.regular_payment_date, 'MM')) between xxpay_util.get_tax_year(pbg.legislation_code, :P_TAX_YEAR, 'start')
and xxpay_util.get_tax_year(pbg.legislation_code, :P_TAX_YEAR, 'end')
group by trunc(decode(pbg.legislation_code, 'ZA', ptp.pay_advice_date, ptp.regular_payment_date), 'MM')
order by 1
) sub
) tm,
(
select distinct petf.element_type_id
from pay_element_classifications pec,
pay_element_types_f petf
where instr('|' || :PRIMARY_CLASS_IDS || '|', '|' || to_char(nvl(pec.parent_classification_id, -6969)) || '|') > 0
and
(
petf.classification_id = pec.classification_id
or instr('|' || :PRIMARY_CLASS_IDS || '|', '|' || to_char(nvl(petf.classification_id, -6969)) || '|') > 0
)
) ele,
pay_element_types_f petf,
pay_input_values_f pivf,
pay_run_result_values prrv,
pay_run_results prr,
pay_assignment_actions paa,
pay_payroll_actions ppa
where petf.element_type_id = ele.element_type_id
and tm.real_month_end between petf.effective_start_date and petf.effective_end_date
and pivf.element_type_id = petf.element_type_id
and pivf.name = 'Pay Value'
and prrv.input_value_id = pivf.input_value_id
and nvl(prrv.result_value, '0') <> '0'
and prr.run_result_id = prrv.run_result_id
and prr.status in ('P', 'PA') -- RUN_RESULT_STATUS lookup
and prr.element_type_id = petf.element_type_id
and paa.assignment_action_id = prr.assignment_action_id
and paa.action_status = 'C'
and paa.assignment_id = :ASSIGNMENT_ID
and ppa.payroll_action_id = paa.payroll_action_id
and ppa.action_status = 'C'
and ppa.action_type in ('B', 'F', 'G', 'I', 'O', 'Q', 'R', 'V', 'L') -- = 'R'
and ppa.effective_date between tm.real_month_start and tm.real_month_end
and petf.element_name in ('ZA_Tax_Costing', 'ZA_UIF_Employee_Contribution')
group by petf.element_name,
tm.i
) sub2
group by sub2.element_name

Query Runs Slower The Second Time

I have this massive query that I can typically run in under 2 minutes. However, when I run it a second time about a minute after, it goes on infinitely... so I kill the process and my SSMS session. I don't have any other jobs running in the background.
Is something else being retained on the server? I think I'm missing something as far as how SQL Server works.
Thanks.
EDIT: Here's the SQL (had to do a little obfuscation)
SELECT pl.OrangeLocationID ,
e.EventID ,
cr.Title AS [Event Type] ,
su.LastName + ', ' + su.FirstName AS FMR ,
CONVERT(VARCHAR(20), pl.Report_Date, 101) AS [Report Entry Date] ,
l.Name ,
l.Number ,
ll.SodaPopLocationID AS [SodaPop Location ID] ,
l.Zip ,
c.Channel ,
pl.DT AS [ReportedDate] ,
RIGHT(pl.DT_start, 8) AS [ReportedStartTime] ,
RIGHT(pl.DT_end, 8) AS [ReportedEndTime] ,
[CMS].dbo.dDateDiff(pl.DT_start, pl.DT_end) AS [ReportedDuration] ,
pl.scheduled_date AS [ScheduledDate] ,
RIGHT(pl.scheduled_start, 8) AS [ScheduledStartTime] ,
RIGHT(pl.scheduled_end, 8) AS [ScheduledEndTime] ,
[CMS].dbo.dDateDiff(pl.scheduled_start, pl.DT_end) AS [ScheduledDuration] ,
e.HoursPaid AS [Rep Hours Worked] ,
ISNULL(PP.[RepCount], 0) AS [RepCount] ,
CASE WHEN [CMS].dbo.dDateDiff(pl.DT_start, pl.DT_end) = ( e.HoursPaid / ISNULL(PP.[RepCount], 1) )
THEN [CMS].dbo.oa_HourDateDiff(pl.DT_start, pl.DT_end)
WHEN [CMS].dbo.dDateDiff(pl.scheduled_start, pl.DT_end) = ( e.HoursPaid / ISNULL(PP.[RepCount], 1) )
THEN [CMS].dbo.oa_HourDateDiff(pl.scheduled_start, pl.DT_end)
ELSE ( e.HoursPaid / ISNULL(PP.[RepCount], 1) )
END AS [FinalDuration] ,
g.[Description] AS [OA Market] ,
g.SodaPop_Region AS [SodaPop Region] ,
g.SodaPop_Area AS [SodaPop Area] ,
coup4 ,
coupo ,
coupo_e ,
card_num ,
promo ,
promo_no ,
promo_no_o ,
highlight1 ,
highlight2 ,
highlight3 ,
mgmt_reaction ,
mgmt_reaction_e ,
comm_p ,
comm_n ,
r.comments ,
s_fname ,
s_lname ,
v_title ,
ll.KeyAccountCorp AS [Key Account Corp.] ,
interact_new + interact_rep AS [interact_total] ,
samp_new + samp_rep AS [samp_total] ,
purch_new + purch_rep AS [purch_total] ,
23 / ( NULLIF(( interact_new + interact_rep ), 0) * 1.0 ) AS [Int_Crate] ,
CASE WHEN sampletype = 11 THEN ( purch_new + purch_rep ) / ( NULLIF(( samp_new + samp_rep ), 0) * 1.0 )
ELSE NULL
END AS [Samp_Crate] ,
coup1 + coup2 AS [coup_total] ,
CASE WHEN coup1 + coupo > 0 THEN 1
ELSE 0
END AS [CoupDist] ,
DATEPART(month, pl.DT) AS [Visit_Month] ,
DATEPART(quarter, pl.DT) AS [Quarter] ,
DATEPART(weekday, pl.DT) AS [Weekday] ,
CASE DATEPART(weekday, pl.DT)
WHEN 6 THEN 'Fri'
WHEN 7 THEN 'Sat'
WHEN 1 THEN 'Sun'
ELSE 'Mon-Thurs'
END AS [Weekday_Grouped] ,
CASE WHEN dbo.Exception(pl.OrangeLocationID, 12) = 1
OR dbo.Exception(pl.OrangeLocationID, 13) = 1
OR dbo.Exception(pl.OrangeLocationID, 14) = 1 THEN 1
ELSE 0
END AS [EVolume] ,
CASE WHEN dbo.DoesHaveException(pl.OrangeLocationID, 18) = 1 THEN 1
ELSE 0
END AS [CVolume] ,
CASE WHEN dbo.eException(pl.OrangeLocationID, 9) = 1
OR dbo.eException(pl.OrangeLocationID, 22) = 1 THEN 1
ELSE 0
END AS [Volumes] ,
CASE WHEN dbo.eException(pl.OrangeLocationID, 8) = 1
OR dbo.eException(pl.OrangeLocationID, 21) = 1 THEN 1
ELSE 0
END AS [Sales Price] ,
CASE WHEN dbo.eException(pl.OrangeLocationID, 11) = 1 THEN 1
ELSE 0
END AS [Sample Volume] ,
ISNULL(i.[NormalizedSold], 0) AS [EQBottlesSold] ,
CASE WHEN ISNULL(purch_new, 0) = 0 THEN 0
ELSE ISNULL(i.[NormalizedSold], 0) / ( purch_new + purch_rep )
END AS [EQBottlesSoldPerPurch] ,
ac.AvgSales ,
ac.STDEVSales ,
( ISNULL(i.[NormalizedSold], 0) - ac.AvgSales ) / ac.STDEVSales AS [sl] ,
ac.AvgPurchasers ,
ac.STDEVPurchasers ,
( ISNULL(r.purch_new, 0) - ac.AvgPurchasers ) / ac.STrchasers AS [ZScore_Purchasers] ,
ac.AvgConversions ,
ac.STDEVConversions ,
( ISNULL(( purch_new + purch_rep ) / ( NULLIF(( interact_new ), 0) ), 0) - ac.AvgConversions )
/ ac.STDEVConversions AS [ZScore_Conversions] ,
ac.[AvgSalesPerPurchaser] ,
ac.[STDEVSalesPerPurchaser] ,
( ISNULL(( CASE WHEN ISNULL(purch_new, 0) = 0 THEN 0
ELSE ISNULL(i.[NormalizedSold], 0) / ( purch_new + purch_rep )
END ), 0) - ac.[AvgSalesPerPurchaser] ) / ac.[STDEVSalesPerPurchaser] AS [SalesPerPurchaser] ,
( ( ( ISNULL(i.[NormalizedSold], 0) - ac.AvgSales ) / ac.STDEVSales )
+ ( (ISNULL(( CASE WHEN ISNULL(purch_new + purch_rep, 0) = 0 THEN 0
ELSE ISNULL(i.[NormalizedSold], 0) / ( purch_new + purch_rep )
END ), 0) - ac.[AvgSalesPerPurchaser]) ) ) / 4 AS [core] ,
( ( (( ISNULL(i.[NormalizedSold], 0) - ac.AvgSales ) / ac.STDEVSales) ) / 4 ) + 3 AS [core] ,
su.aaUserID ,
l.LsocationID
FROM [CMS_SodaPop].dbo.Schedule pl WITH ( NOLOCK )
INNER JOIN [CMS_SodaPop].dbo.Report r WITH ( NOLOCK ) ON r.OrangeLocationID = pl.OrangeLocationID
INNER JOIN [CMS].dbo.Users su WITH ( NOLOCK ) ON su.UserID = pl.Rep_FMR
INNER JOIN [CMS].dbo.Locations l WITH ( NOLOCK ) ON l.LocationID = pl.LocationID
INNER JOIN [CMS].dbo.OrangeReports cr WITH ( NOLOCK ) ON cr.RedID = pl.RedID
INNER JOIN [CMS_SodaPop].dbo.Events e WITH ( NOLOCK ) ON e.OrangeLocationID = pl.OrangeLocationID
INNER JOIN [CMS_SodaPop].dbo.MarketList g WITH ( NOLOCK ) ON g.GroupID = pl.GroupID
INNER JOIN [CMS_SodaPop].dbo.Locations ll WITH ( NOLOCK ) ON ll.LocationID = pl.LocationID
LEFT JOIN [CMS_SodaPop].dbo.Channels c WITH ( NOLOCK ) ON ll.ChannelID = c.ChannelID
LEFT JOIN ( SELECT PLocationID ,
COUNT(DISTINCT UserID) AS [RepCount]
FROM [CMS_roll].dbo.rollItems WITH ( NOLOCK )
WHERE RedID = 154
GROUP BY OrangeLocationID
) PP ON PP.OrangeLocationID = pl.OrangeLocationID
LEFT JOIN ( SELECT OrangeLocationID ,
SUM(NormalizedSold) AS [NormalizedSold]
FROM [Analysis].dbo.[vSodaPop_Retail_Inventory] WITH ( NOLOCK )
GROUP BY OrangeLocationID
) i ON i.OrangeLocationID = pl.OrangeLocationID
LEFT JOIN [Analysis].dbo.[vSodaPop_Calculations] ac WITH ( NOLOCK ) ON ac.[Quarter] = CASE WHEN DATEPART(MM,
[DT]) IN ( 10,
11, 12 ) THEN 4
END
AND ac.[Year] = DATEPART(YY, pl.DT)
WHERE pl.Activity = 1
AND pl.RedID = 154
AND pl.GroupID <> 444
AND pl.[DT] < GETDATE()
AND DATEPART(YY, [DT]) >= 2010
AND ISNULL(i.NormalizedSold, 0) >= 0
AND DATEPART(year, GETDATE()) = DATEPART(year, r.Insert_Date)
Would have to see the query to really dig in however..
you could try adding OPTION (RECOMPILE) to the end of the query to force it to create a new execution plan.
Are you using Temp Tables?
Cursors not deallocated & closed?
You can look at Profiler to see if anything looks different between the 2 executions.
Are you sure it isn't being blocked by another process the second time?
What happens if you execute
CHECKPOINT;
GO;
DBCC DROPCLEANBUFFERS;
GO;
DBCC FREEPROCCACHE;
GO;
between the queries? This is not really a solution but will help diagnosis.