DataTable:
Any time an ID has a record where Type = A
and a record where Type = B
, I want to not include the record where Type = A
.
SELECT * FROM DataTable WHERE Type <> 'A'
This query does not work, because I still want to show the record where Type = A
for ID 500.
Advertisement
Answer
You can use count()
window analytic function with partitioning by ID
column and then filter out rows with type equals A
whenever count is greater than one :
with t2 as ( select t.*, count(*) over (partition by ID order by ID,Qty ) as cnt from t ) select type,ID,Qty from t2 where ( type <> 'A' and cnt > 1 ) or cnt <= 1