Oracle SQL : Intersect tuple literals -
is there function or operator or otherwise simple(r) construct obtain intersection of 2 tuple literals in oracle sql?
taking consideration following example:
having following table
------------------------------ table sometable ------------------------------ id | telephone | mobile | fax ------------------------------ 1 | 123 | 456 | 789 given list of n numbers { n1, n2, n3, ... , n } find id, such that:
telephone = n1 or mobile = n1 or fax = n1 or telephone = n2 or mobile = n2 or fax = n2 or telephone = n3 or mobile = n3 or fax = n3 .... or telephone = n or mobile = n or fax = n two plausible solutions are:
1. solution 1
select id sometable n1 in (telephone, mobile, fax) or n2 in (telephone, mobile, fax) or n3 in (telephone, mobile, fax) .... or n in (telephone, mobile, fax) ; 2. solution 2
select id sometable telephone in (n1, n2, n3, ..., n) or mobile in (n1, n2, n3, ..., n) or fax in (n1, n2, n3, ..., n) ; however there function / operator following?
select id sometable intersect_function ( (telephone, mobile, fax), (n1, n2, n3, ..., n) ) = true ; an alternative, simpler construct welcomed, taking consideration condition part of longer query more numerous , possibly more complex conditions.
thanks.
my idea convert search numbers table via with clause:
then little regexp trick can create, single row, 1 row per value, , match them against table in clause:
create table sometable ( id number, telephone number, mobile number, fax number ); insert sometable values(1, 123, 456, 789); insert sometable values(2, 0, 0, 123); insert sometable values(3, 456, 0, 0); w(n) ( select regexp_substr('123, 456', '\d+', 1, level) n dual connect regexp_instr('123, 456', '\d+', 1, level) != 0 ) select * sometable s, w w.n in (s.telephone, s.mobile, s.fax) ; this gives expected:
id telephone mobile fax n 1 123 456 789 123 2 0 0 123 123 1 123 456 789 456 3 456 0 0 456
Comments
Post a Comment