sql - Oracle using correct filter -
i got sql-statement far:
select * table1, table2 table1.fk = table2.id , table2.id = 4 which gives me following output:
[...] [...] rights [...] [...] ... ... ( id in ( 10 , 700000 , 80 , 5 ) ) ... ... ... ... ( id in ( 500000 , 10 , 80 , 5 ) ) ... ... ... ... ( id in ( 10 , 5 , 80 , 900000 ) ) ... ... i need write in where shows me rows in numbers in rights example 700000 or below.
in case row 1 , 2 should shown.
any ideas how solve this?
this might work out need do:
with t1 (select 1 fk dual union select 2 fk dual union select 3 fk dual), t2 (select 1 id, 'id in ( 10 , 700000 , 80 , 5 )' rights dual union select 2 id, 'id in ( 500000 , 10 , 80 , 5 )' rights dual union select 3 id, 'id in ( 10 , 5 , 80 , 900000 , 203 )' rights dual), pivoted_t2 (select t2.id, t2.rights, rtrim(ltrim(upper(t2.rights), 'id n('), ' )') rights_stripped, to_number(trim(regexp_substr(rtrim(ltrim(upper(t2.rights), 'id n('), ' )'), '[^,]+', 1, level))) rights_item, max(level) on (partition t2.id) num_items t2 connect prior t2.id = t2.id , prior dbms_random.value not null , trim(regexp_substr(rtrim(ltrim(upper(t2.rights), 'id n('), ' )'), '[^,]+', 1, level)) not null) select t1.fk, pt2.rights t1 inner join pivoted_t2 pt2 on (t1.fk = pt2.id) pt2.rights_item <= 700000 group t1.fk, pt2.rights, pt2.num_items having count(*) = pt2.num_items; fk rights ---------- ------------------------------------ 1 id in ( 10 , 700000 , 80 , 5 ) 2 id in ( 500000 , 10 , 80 , 5 ) basically, you'll need split out rights individual items, work out how many items there in total each set of rights, , work out how many below specified value. if same number of values returned in set, know items match filter.
eta: can't guarantee perform well, large amount of data, if you're going insist on storing data in manner, you'll have accept performance suffer.
Comments
Post a Comment