SQLTeam.com | Weblogs | Forums

Record Selection


Data is stored in my table is follows;

id Date user option
12345 09/26/2016 serv Y
12345 09/26/2016 serv N

How do I select the record where option = Y? If there is no record equal to Y, return the record = N


create table #ertweety(d int not null, Date datetime, [user] varchar(50), [option] char(1))

insert into #ertweety
select '12345', '09/26/2016', 'serv', 'Y' union
select '12345', '09/26/2016', 'serv', 'N'

if not exists (select 1 from #ertweety where [option] = 'Y')
select * from #ertweety where [option] = 'N'
select * from #ertweety where [option] = 'Y'

drop table #ertweety


Doesn't seem to work for me. I should have stated I'm using pl/sql in an oracle environment.


this forum is for microsoft sql server


Select *
From table t1
Where t1.option = 'Y'
Or (t1.option = 'N' And Not Exists (Select * From table t2 Where t2.id = t1.id and t2.date = t1.date and t2.option = 'Y'))