Solved

query contd

Posted on 2014-01-10
8
178 Views
Last Modified: 2014-01-15
In continuation with the below query:

http://www.experts-exchange.com/Database/Oracle/Q_28335810.html


Sdstuber..Is there a way to get result it as one value rather than multiple records

I just want the result as a value not the records, the above example gives me 3 records instead of record with:
TGT.COL_B = SRC.COLB
TGT.COL_C = SRC.COL_C
TGT.COL_D = SRC.COL_D

Thanks
0
Comment
Question by:gs79
  • 5
  • 3
8 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39772762
try this...


SELECT id, listagg('TGT.' || str || ' = ' || 'SRC.' || str,' ') within group( order by str)
  FROM (SELECT id,
               REGEXP_SUBSTR(
                   col_str,
                   '[^ ,]+',
                   1,
                   n
               )
                   str
          FROM yourtable,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(
                   col_str,
                   '[^ ,]+',
                   1,
                   n
               )
                   IS NOT NULL
        MINUS
        SELECT id,
               REGEXP_SUBSTR(
                   key,
                   '[^ ,]+',
                   1,
                   n
               )
          FROM yourtable,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(
                   key,
                   '[^ ,]+',
                   1,
                   n
               )
                   IS NOT NULL) 
group by id

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39772777
I aggregated them above with a space between each.
If you want them in a single string but each on their own line

then change the ' ' in LISTAGG to chr(10)


LISTAGG('TGT.' || str || ' = ' || 'SRC.' || str, chr(10))

on windows you might want to use

LISTAGG('TGT.' || str || ' = ' || 'SRC.' || str, chr(13) || chr(10))

example of chr(10) below


SELECT id, listagg('TGT.' || str || ' = ' || 'SRC.' || str,' ') within group( order by str)
  FROM (SELECT id,
               REGEXP_SUBSTR(
                   col_str,
                   '[^ ,]+',
                   1,
                   n
               )
                   str
          FROM yourtable,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(
                   col_str,
                   '[^ ,]+',
                   1,
                   n
               )
                   IS NOT NULL
        MINUS
        SELECT id,
               REGEXP_SUBSTR(
                   key,
                   '[^ ,]+',
                   1,
                   n
               )
          FROM yourtable,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(
                   key,
                   '[^ ,]+',
                   1,
                   n
               )
                   IS NOT NULL)
group by id 

Open in new window

0
 

Author Comment

by:gs79
ID: 39775788
I am also trying to generate the onclause for the merge query which is basically second part of the minus query. I am getting the results as follows:

TGT.COL_A = SRC.COL_A
TGT.COL_B = SRC.COL_B
TGT. = SRC.
TGT. = SRC.

here is the query I am using:
SELECT  listagg('TGT.' || str || ' = ' || 'SRC.' || str,chr(13)) within group( order by str)
from(
SELECT REGEXP_SUBSTR(key, '[^ ,]+',1,n) str
          FROM drop_test_tab,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(col_str, '[^ ,]+',1,n)  IS NOT NULL
)  

I am getting 4 rows with last two rows as blank from the inner query. I think 'IS NOT NULL' is intended to filter out the blank rows which is not happening

SELECT REGEXP_SUBSTR(key, '[^ ,]+',1,n) str
          FROM drop_test_tab,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(col_str, '[^ ,]+',1,n)  IS NOT NULL

The inner query gives the following result:
COL_A
COL_B
NULL
NULL

Please let me know how to resolve this..

Thanks
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39776427
you have 2 different first parameters in the REGEXP_SUBSTR


key vs col_str


key should be in both parts of one half of the MINUS
and col_str should be in both parts of the other half


don't put both in both
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:gs79
ID: 39777376
Thanks..

It worked..

Is there a way to display as comma seperated. I can append a comma but it displays on the last line which is not needed:

TGT.COL_B = SRC.COL_B ,
TGT.COL_C = SRC.COL_C ,
TGT.COL_D = SRC.COL_D

Thanks
0
 

Author Comment

by:gs79
ID: 39777410
further massaging the listagg output with substr /length function i was able to remove the comma in the end..

Thanks
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39777417
just add the comma to the aggregate function's delimiter parameter  (the 2nd parameter)

so, if you're using chr(13) as your delimiter
then change...

  listagg('TGT.' || str || ' = ' || 'SRC.' || str, chr(13))

to

  listagg('TGT.' || str || ' = ' || 'SRC.' || str, ',' || chr(13))



no need to do any substr/length parsing, let the aggregate do it for you
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39777559
Do you need anything else?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now