Вот два аналогичных способа сделать это.
declare @SemiDupe table (ID int, name varchar(100), Y_N varchar(1), Paid money, description varchar(100));
insert into @SemiDupe
SELECT 4545 AS ID, 'bob smith' AS name, 'Y' AS Y_N, 15.39 AS Paid, 'therapy' as description UNION ALL
SELECT 4545 AS ID, 'bob smith' AS name, 'N' AS Y_N, 0 AS Paid, '' as description UNION ALL
SELECT 1847 AS ID, 'sara tam' AS name, 'N' AS Y_N, 0 AS Paid, '' as description
;
-- corrected SQL
Select *
From @SemiDupe as A
Left Join (Select ID,
Sum(Case When Y_N = 'Y' Then 1 Else 0 End) as CountY
From @SemiDupe Group by ID) as B
On A.ID = B.ID
Where Y_N = 'Y'
Or (Y_N = 'N' and CountY = 0)
-- Another way
Select *
From @SemiDupe as A
Left Join (Select ID, MAX(Y_N) as maxY_N
From @SemiDupe Group by ID) as B
On A.ID = B.ID
Where Y_N = 'Y'
Or (Y_N = 'N' and maxY_N = 'N')
Результаты - (просто выберите столбцы A. *, чтобы обрезать результаты вместо *)
ID name Y_N Paid description ID CountY
4545 bob smith Y 15.39 therapy 4545 1
1847 sara tam N 0.00 1847 0
Внимание- Это не помогает в случаях, когда есть несколько строк Y - все они будут перечислены.