Oracle TRICk Question


 
Thread Tools Search this Thread
Top Forums Programming Oracle TRICk Question
# 1  
Old 12-12-2012
Oracle TRICk Question

HI Guys ,

Below are the two columns ITEMS and STATE of table .

Code:
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

Last edited by Scott; 12-12-2012 at 11:02 AM.. Reason: Code tags, please...
# 2  
Old 12-12-2012
Let me ask you a question, can you tell me the source of values for columns ITEMS and STATE? Are you fetching these values from any table or file?
Code:
'2345','ggdh','k5tg','dgyt','hFF' DF 
'1234','ghyt','DDD','GHTD','ABF' PQ

# 3  
Old 12-12-2012
HI,
I am calling from table .
# 4  
Old 12-12-2012
Then why don't you fetch it directly from table in your PL/SQL?
Code:
STATE='DF' and ITEMS in (select distinct ITEMS from my_table)

# 5  
Old 12-12-2012
HI BIpi,

The output from that table is already displayed . I just want the output to come in this frame based on states .

Code:
V_variable= (STATE='DF' and ITEMS in ('2345','ggdh','k5tg','dgyt','hFF') OR STATE='PQ' and ITEMS in ('1234','ghyt','DDD','GHTD','ABF') )

Something like it should iterate based on state and items .
Thanks.

Last edited by Scott; 12-14-2012 at 01:18 PM.. Reason: Code tags
# 6  
Old 12-12-2012
Use the xmlagg function to aggregate multiple rows onto one column:
Code:
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')


Last edited by Yoda; 12-12-2012 at 01:37 PM.. Reason: Modified regex
# 7  
Old 12-14-2012
Hi BIPI ,

Code:
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;

whare should i keep ITEMS values of '1234','ghyt','DDD','GHTD','ABF' in above query ??
Also add OR inbetween two outputs . Like
Code:
(STATE='DF' and ITEMS in ('2345','ggdh','k5tg','dgyt','hFF') OR STATE='PQ' and ITEMS in ('1234','ghyt','DDD','GHTD','ABF') )


Last edited by Scott; 12-14-2012 at 01:19 PM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX trick or command

Hi, Is there any command to do this -- Input is -- Ant Bat Cat Dog Output is -- A_Ant B_Ant A_Bat B_Bat A_Cat B_Cat A_Dog B_Dog (12 Replies)
Discussion started by: Indra2011
12 Replies

2. UNIX for Dummies Questions & Answers

Vi search starting from line n --- any trick for this ???

Hi all, It does not seem to be possible to use vi to search from text from line n forward or backward or is there a not well known vi trick to do that? What I am trying to do is for example I am on line 100 and I want to search from line 100 backward or forward for a specific text. Using /... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. UNIX for Dummies Questions & Answers

alternative to the grep trick

Hi, We used to use the below commands often. ps -ef|grep bc ps -ef|grep abc|grep -v grep Both fairly returns the same result. For example, the process name is dynamic and we are having the process name in a variable, how we can apply the above trick. For example "a" is the... (11 Replies)
Discussion started by: pandeesh
11 Replies

4. UNIX for Dummies Questions & Answers

A trick to avoid ESC in vim

Hello, I find a trick to avoid pressing ESC without key-maping in vim. I am pleasure using this method, because ALT key is very comfortble for thumb to press. What's the trick? the ALT key. When you are in INSERT mod, press ALT+l switch to COMMAND mod without... (2 Replies)
Discussion started by: vistastar
2 Replies

5. UNIX and Linux Applications

Oracle DB Question

Hi, Where do i post Oracle DB related questions?? I basically have 2 questions - 1. What is pmon and smon? Difference between them? If either or both process fail then how can we monitor them? 2. Is lsnrctl same as tnslsnr? if i want to check for listener status then how would i do... (2 Replies)
Discussion started by: Mr. Zer0
2 Replies

6. Shell Programming and Scripting

Any trick to speed up script?

Hi Guys, I have a script that I am using to convert some text files to xls files. I create multiple temp. files in the process of conversion. Other than reducing the temp. files, are there any general tricks to help speed up the script? I am running it in the bash shell. Thanks. (6 Replies)
Discussion started by: npatwardhan
6 Replies

7. UNIX for Dummies Questions & Answers

Looking for tips ant trick extend vi

Hello im looking for sites or tutorials how to extend vi (not vim) for programming and scripting beyond simple editing . Thanks allot (0 Replies)
Discussion started by: umen
0 Replies

8. Shell Programming and Scripting

Stupid find trick

At work, we use a software development product (from a company that will remain nameless, but whose name may be considered a synonym for "logical"). The development trees are organized beneath a top directory, let's call it "$rat". The first level under $rat contains the major system names, and... (2 Replies)
Discussion started by: criglerj
2 Replies
Login or Register to Ask a Question