0

I have requirement to select a data from single table. The requirement is as follows Select data from table which has one value but not another. for e.g I want to select distinct ID which has value 1 but not value 8 present. For for above e.g the desired o/p should be

ID Value
123 1
123 8
234 8
456 1
876 5
876 1
765 8
765 5
O/p ID
456
876

Also my table contains 500K record so query should be able to execute with good performance Any help regarding it as i am stuck.

asked Nov 20, 2015 at 16:40
3
  • Yes i tried but didnt post it because i thought i am way off. Appreciate your reponse but i posted it after trying. select a.id from a.tableone a, (select c.id, count(1)from tableone c group by c.id having count(1) > 1)b where a.id = b.id and a.id = 1 and a.id <> 8 Commented Nov 20, 2015 at 16:50
  • Why does it have to be a self join? Commented Nov 20, 2015 at 16:59
  • As i need to match data within same table and depending on it group. I may be wrong. If you suggest any approach i can try myself. Commented Nov 20, 2015 at 17:03

3 Answers 3

1

Here's how i got it to work:

create table tableone (id int, value int)
insert into tableone values (123, 1);
insert into tableone values (123, 8);
insert into tableone values (234, 8);
insert into tableone values (456, 1);
insert into tableone values (876, 5);
insert into tableone values (876, 1);
insert into tableone values (765, 8);
insert into tableone values (765, 5);
Select a.*
from tableone a
left join tableone X on a.id = x.id and x.value = 8
Where a.value = 1
and X.id is null
drop table tableone
answered Nov 20, 2015 at 16:59
4
  • Hi John, Thank you for the help it worked. I have miles to go in SQL queries. Commented Nov 20, 2015 at 17:14
  • take it one step at a time. Lots of research and patience ;-) Commented Nov 20, 2015 at 17:24
  • You should not even need the subquery. Commented Nov 20, 2015 at 19:33
  • @jkavalik you're right... fixed the query. Commented Nov 23, 2015 at 16:24
0

except

select id from from table where value = 1 
except 
select id from from table where value = 8
answered Nov 20, 2015 at 17:05
0

You might try a conditional aggregate:

select id
from table
where value in (1,8) -- 1 or 8
group by id
having sum(case when value = 8 then 1 else 0 end) = 0 -- only 1, no 8

or a NOT EXISTS:

select id
from table as t1
where value = 1 -- 1
and not exists -- but no 8
 ( select * 
 from table as t2
 where t1.id = t2.id
 and t2.value = 8 
 )

Which on is more efficient depends on your actual data and existing indexes...

answered Nov 20, 2015 at 17:11
1
  • Hi Thank for the response. I have soo many different queries to address my issue. This forum is awesum. I have just started writing SQL queries and all this input will help me. Commented Nov 20, 2015 at 17:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.