0

I need to compare multiple values of a table with same set of values in single shot without using multiple AND condition in WHERE clause. Please let me know is any methods to do it.

code:

select count(1)
  from test
 where pre_sys_id1 in (select sys_id from test where auto_id = 10)
   and pre_sys_id2 in (select sys_id from test where auto_id = 10)
   and pre_sys_id3 in (select sys_id from test where auto_id = 10)
   and pre_sys_id4 in (select sys_id from test where auto_id = 10)
;
1
  • How your output should look? Commented Nov 23, 2016 at 7:35

2 Answers 2

1
Select count(1)
from test
where  (select sys_id from test where auto_id=10) = all (pre_sys_id1,pre_sys_id2,pre_sys_id3,pre_sys_id4) 

Or join

Select count(1)
from test t1
join test t2 on( sys_id= pre_sys_id1 and sys_id= pre_sys_id2 and sys_id= pre_sys_id3  and sys_id= pre_sys_id4) 
where t2.auto_id = 10 ;
Sign up to request clarification or add additional context in comments.

2 Comments

What if (select sys_id from test where auto_id=10) returns multiple rows?
Exception will be raised ( single-row subquery returns more than one row). Construction with all/some works only for scalar values. one_value =,<,> ALL/SOME (set of values)
1

You can try:

select count(1)
  from test
 where (pre_sys_id1, pre_sys_id2, pre_sys_id3, pre_sys_id4)
   in (select sys_id, sys_id, sys_id, sys_id from test where auto_id = 10)

Comments

Your Answer

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.