SQLTeam.com | Weblogs | Forums

Record Selection


#1

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


#2

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')
begin
select * from #ertweety where [option] = 'N'
end
else
begin
select * from #ertweety where [option] = 'Y'
end

drop table #ertweety


#3

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


#4

this forum is for microsoft sql server


#5

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'))