0

I need to remove ALL instances of PaymentId If Label is "Error".

CREATE TABLE Test (Id INT, PaymentId INT, Label VARCHAR(25)); INSERT INTO Test VALUES (1, 22, 'Error'), (2, 22, 'Seattle'), (3, 22, 'Pending'), (4, 33, 'Paid'), (5, 33, 'Los Angeles'), (6, 44, 'Houston'), (7, 44, 'Error'), (8, 55, 'Pending'), (9, 55, 'San Diego'), (10, 55, 'Authorization') SELECT * FROM Test 
Id PaymentId Label
1 22 Error
2 22 Seattle
3 22 Pending
4 33 Paid
5 33 Los Angeles
6 44 Houston
7 44 Error
8 55 Pending
9 55 San Diego
9 55 Authorization

Expected Output:

Id PaymentId Label
4 33 Paid
5 33 Los Angeles
8 55 Pending
9 55 San Diego
9 55 Seattle

2 Answers 2

1

Just another option via a CTE and the window function sum() over()

;with cte as ( SELECT * ,Flg = sum( case when Label='Error' then 1 end) over (partition by PaymentID) FROM Test ) Delete from cte where Flg >=1 

The Updated Table

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You can use IN.

delete from Test where PaymentId in (select PaymentId from test where Label='Error' ); 

DBFiddle demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.