Solved

query contd

Posted on 2014-01-10
8
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 74

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 74

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

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
 

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 74

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 74

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many‚Ķ
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to take different types of Oracle backups using RMAN.

691 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