We have been inserting transaction payments incorrectly for the last 6 months. We need to insert broken down payments, but for some cases, we have been inserting payments summing them up.
How some payments were inserted incorrectly:
How the same transaction should have its payments inserted:
What I have done so far to identify the incorrect payments was to run the incorrect query and retrieve all the records starting on 12/01/2019, then save those records in a temporary table.
Query using the incorrect table:
SELECT DISTINCT
CAST(P.ExternalId AS CHAR(25)) AS 'transID'
, CASE RTP.Activity
WHEN 0 THEN 'Payment'
WHEN 1 THEN 'Void'
END AS 'transType'
, P.TransactionCreateDate AS 'createdDate'
, P.Amount AS 'paymentAmount' <<< Payment column was being retrieved from wrong table.
, P.EffectiveDate AS 'paymentDate'
, CAST(P.Id AS NVARCHAR(50)) AS 'payment_Id'
, P.TransactionBatchId AS 'batchId'
, T.TenderTypeId AS 'TenderType'
, CAST(TType.Name AS CHAR(10)) AS 'PaymentType'
FROM
Payments P
INNER JOIN dbo.Tenders T
ON T.TransactionId = P.TransactionId
INNER JOIN dbo.TenderTypes TType
ON TType.Id = T.TenderTypeId
WHERE
CAST(P.TransactionCreateDate AS DATE) >= '12/01/2019'
ORDER BY
createdDate DESC
Then I make corrections to the query, to retrieve all the transactions starting 12/01/2019 but getting the broken down payments this time, and putting them in a second temporary table. The difference are 955 records.
Query using the correct table to get payment amount:
SELECT DISTINCT
CAST(P.ExternalId AS CHAR(25)) AS 'transID'
, CASE RTP.Activity
WHEN 0 THEN 'Payment'
WHEN 1 THEN 'Void'
END AS 'transType'
, P.TransactionCreateDate AS 'createdDate'
, T.Amount AS 'paymentAmount'
, P.EffectiveDate AS 'paymentDate'
, CAST(P.Id AS NVARCHAR(50)) AS 'payment_Id'
, P.TransactionBatchId AS 'batchId'
, T.TenderTypeId AS 'TenderType'
, CAST(TType.Name AS CHAR(10)) AS 'PaymentType'
FROM
Payments P
INNER JOIN dbo.Tenders T
ON T.TransactionId = P.TransactionId
INNER JOIN dbo.TenderTypes TType
ON TType.Id = T.TenderTypeId
WHERE
CAST(P.TransactionCreateDate AS DATE) >= '12/01/2019'
ORDER BY
createdDate DESC
My question is how can I now compare the contents of each table, to extract the ones that need to be corrected? I am sure this is not difficult, I am just overwhelmed and have not being able to figure it out and was wondering if someone could offer a helping hand.
Thank you much.