WITH cte
AS(
SELECT twt.File_Name FROM Trapeze_Work_Table twt
EXCEPT
SELECT acd.FileName FROM APP_CSV_Data acd
)
DELETE FROM Trapeze_Work_Table WHERE File_Name IN (SELECT x.File_Name FROM cte as x)
WITH ctee
AS
(SELECT Year,Month, xxy = CASE Month
WHEN 'JANUARY' THEN '1-1-' + Year
WHEN 'Feuary' THEN '2-1-' + Year
WHEN 'March' THEN '3-1-' + Year
WHEN 'April' THEN '4-1-' + Year
WHEN 'May' THEN '5-1-' + Year
WHEN 'June' THEN '6-1-' + Year
WHEN 'July' THEN '7-1-' + Year
WHEN 'August' THEN '8-1-' + Year
WHEN 'September' THEN '9-1-' + Year
WHEN 'October' THEN '10-1-' + Year
WHEN 'November' THEN '11-1-' + Year
WHEN 'December' THEN '12-1-' + Year
end
from DimTransactionSatByMonth)
UPDATE DimTransactionSatByMonth SET MonthDay = ctee.xxy
FROM ctee
WHERE ctee.Year = DimTransactionSatByMonth.Year
AND ctee.Month = DimTransactionSatByMonth.Month
WITH ctee
AS
(
SELECT apsa.PAYMENT_SCHEDULE_ID FROM AR_PAYMENT_SCHEDULES_ALL apsa
EXCEPT
SELECT ta.keyField FROM TmpARKey ta)
DELETE FROM AR_PAYMENT_SCHEDULES_ALL
WHERE PAYMENT_SCHEDULE_ID IN (select ctee.PAYMENT_SCHEDULE_ID FROM ctee)
WITH cce2
AS(
SELECT wdd.SOURCE_LINE_ID, wdd.RELEASED_STATUS, COUNT(wdd.SOURCE_LINE_ID) AS LineCnt, COUNT(wdd.RELEASED_STATUS) AS ReleaseCnt FROM WSH_DELIVERY_DETAILS wdd
WHERE wdd.ORGANIZATION_ID = 383 AND wdd.SOURCE_LINE_ID = 5684030
GROUP BY wdd.SOURCE_LINE_ID,wdd.RELEASED_STATUS)
SELECT cce2.SOURCE_LINE_ID, cce2.RELEASED_STATUS, cce2.LineCnt, cce2.ReleaseCnt FROM cce2 -- WHERE cce2.cnt > 1
ORDER BY cce2.SOURCE_LINE_ID DESC
WITH ctee
AS(
SELECT
acra.CASH_RECEIPT_ID,
acra.RECEIPT_NUMBER,
hca.ACCOUNT_NUMBER,
araa.RECEIVABLE_APPLICATION_ID
,cocn.ID
,RANK() OVER (PARTITION BY acra.CASH_RECEIPT_ID ORDER BY araa.RECEIVABLE_APPLICATION_ID) AS rk
,cocn.CashReceiptID
FROM AR_CASH_RECEIPTS_ALL acra
LEFT OUTER JOIN AR_RECEIVABLE_APPLICATIONS_ALL araa
ON araa.CASH_RECEIPT_ID = acra.CASH_RECEIPT_ID
LEFT OUTER JOIN AR_PAYMENT_SCHEDULES_ALL apsa
ON apsa.CASH_RECEIPT_ID = araa.CASH_RECEIPT_ID
LEFT OUTER JOIN HZ_CUST_ACCOUNTS hca
ON hca.CUST_ACCOUNT_ID = apsa.CUSTOMER_ID
INNER JOIN AR.Custome_OnCash_Notes cocn
ON cocn.AccountNumber = hca.ACCOUNT_NUMBER
AND cocn.ReceiptNumber = acra.RECEIPT_NUMBER
)
--SELECT * FROM ctee WHERE ctee.rk = 1 and ctee.CashReceiptID is NULL ORDER BY ctee.ID
UPDATE AR.Custome_OnCash_Notes
set
AR.Custome_OnCash_Notes.CashReceiptID = ctee.CASH_RECEIPT_ID
FROM
ctee
WHERE AR.Custome_OnCash_Notes.ID = ctee.ID