This approach uses ROW_NUMBER() and therefore will only work on SQL Server 2005 or later.
I changed your sample data as it is, all transaction_id have indicator Y !
DECLARE @t TABLE ( transaction_id NCHAR(1), decision_id INT, accepted_ind NCHAR(1) NULL ) INSERT @t VALUES ( 'A' , 1 , NULL ), ( 'A' , 2 , '' ), ( 'A' , 4 , 'Y' ), ( 'B' , 1 , '' ), ( 'B' , 2 , 'N' ), -- change from your sample data ( 'C' , 1 , 'Y' ), ( 'D' , 1 , 'N' ), ( 'D' , 2 , 'O' ), ( 'D' , 3 , 'Y' )
And here is the request itself:
SELECT transaction_id, decision_id, accepted_ind FROM ( SELECT transaction_id, decision_id, accepted_ind, ROW_NUMBER() OVER ( PARTITION BY transaction_id ORDER BY CASE WHEN accepted_ind = 'Y' THEN 1 WHEN decision_id = 1 THEN 2 ELSE 3 END ) rn FROM @t ) Raw WHERE rn = 1
Results:
transaction_id decision_id accepted_ind
The ROW_NUMBER() gives a "priority" to each criterion you specify; we then ORDER BY select the best and take the first row.