Below are the two columns ITEMS and STATE of table .
ITEMS STATE
'2345','ggdh','k5tg','dgyt','hFF' DF
'1234','ghyt','DDD','GHTD','ABF' PQ
Can we get output in PL/SQL in below way ?
STATE='DF' and ITEMS in ('2345','ggdh','k5tg','dgyt','hFF') OR STATE='PQ' and ITEMS in ('1234','ghyt','DDD','GHTD','ABF')
Based on column entries it has to give output in above fashion . Please guide
Use the xmlagg function to aggregate multiple rows onto one column:
SQL> select '(STATE=' || regexp_replace(state,'([[:alpha:]]{2})',q'['\1']') || ' AND
ITEMS in (' || regexp_replace(rtrim (xmlagg (xmlelement (e, items || ',')).extract ('//text()'), ','),'([[:alnum:]]{3,4})',q'['\1']') || ')'
from tmp_state_items group by state;
----------------------------------------------------------------
(STATE='DF' AND ITEMS in ('2345','ggdh','k5tg','hFF','dgyt')
(STATE='PQ' AND ITEMS in ('ABF','1234','ghyt','DDD','GHTD')
Please always use code tag when posting code fragments or data samples.
If you want OR simply append it like I appended AND. I cannot tell where you can keep it, it totally depends upon how you want to implement it. I just gave you a method to form the frame that you requested. I hope this helps.