Hi,@Mansoor Mohammed
Please check this:
CREATE TABLE #TestTable
(
SequenceNo int,
Name varchar(20),
CreditCardNumber varchar(20),
TransactionSequence int,
PaymentVersion int,
Transactions varchar(10),
Comment varchar(100)
);
INSERT INTO #TestTable VALUES
(1, 'xxx', '111111111', 1,1, 'Y', ''),
(2, 'xxx', '111111111', 2,1, 'Y', ''),
(3, 'yyy', '222222222', 1,1, 'Y', ''),
(4, 'yyy', '222222222', 2,1, 'E', ''),
(5, 'yyy', '222222222', 3,1, 'Y', ''),
(6, 'zzz', '333333333', 1,1, 'Y', ''),
(7, 'zzz', '333333333', 2,1, 'Y', ''),
(8, 'bla', '444444444', 1,1, 'Y', ''),
(9, 'bla', '444444444', 2,1, 'E', ''),
(10, 'bla', '444444444', 3,1, 'Y', ''),
(11, 'bla', '444444444', 4,1, 'Y', ''),
(12, 'bla', '444444444', 5,2, 'Y', ''),
(13, 'bla', '444444444', 6,2, 'E', ''),
(14, 'bla', '444444444', 7,2, 'Y', ''),
(15, 'gla', '555555555', 1,1, 'Y', ''),
(16, 'gla', '555555555', 2,1, 'E', ''),
(17, 'gla', '555555555', 3,1, 'Y', ''),
(18, 'gla', '555555555', 4,2, 'Y', ''),
(19, 'gla', '555555555', 5,2, 'E', ''),
(20, 'gla', '555555555', 6,2, 'Y', '');
;WITH CTE AS
(SELECT *,LAG (Transactions) OVER (PARTITION BY CreditCardNumber,PaymentVersion ORDER BY TransactionSequence) AS Lag
FROM #TestTable
WHERE CreditCardNumber IN (SELECT CreditCardNumber FROM #TestTable WHERE Transactions = 'E')
AND TransactionSequence>=2
)
UPDATE CTE
SET Transactions = CASE WHEN Transactions = 'Y' AND Lag IS NOT NULL THEN 'N' ELSE Transactions END,
Comment = CASE WHEN Transactions = 'E' THEN 'Error' ELSE Comment END;
UPDATE #TestTable
SET Comment ='Error in the Previous transaction' WHERE Transactions = 'N';
SELECT * FROM #TestTable
Output:
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.