Since you are brand new around here I create the ddl and sample data for you.
if OBJECT_ID('tempdb..#Data') is not null
drop table #Data
create table #Data
(
RefNumber int,
EntryType tinyint,
EntryAmount int
)
insert #Data
select 239000,0,200 union all
select 239000,0,-200 union all
select 239000,2,-200 union all
select 239000,2,200 union all
select 239000,11,-145 union all
select 239000,11,145 union all
select 236777,0,162 union all
select 236777,0,-162 union all
select 236777,2,-45 union all
select 236777,2,45 union all
select 236777,7,62 union all
select 236777,7,-62 union all
select 236777,11,-162 union all
select 236777,11,162 union all
select 238888,0,345 union all
select 238888,0,-345 union all
select 238888,11,-245 union all
select 238888,11,245
select * from #Data
There are a number of ways this could be accomplished. Here is one of those ways.
;with HasAllThree as
(
Select RefNumber, COUNT(distinct EntryType) as MyCount
from #Data
where EntryType in (0, 2, 11)
group by RefNumber
having COUNT(distinct EntryType) = 3
)
select d.RefNumber
from #Data d
join HasAllThree h on h.RefNumber = d.RefNumber
where d.RefNumber not in
(
Select RefNumber
from #Data
where EntryType not in (0, 2, 11)
)
group by d.RefNumber
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/