Solved

LISTAGG question

Posted on 2013-01-17
6
621 Views
Last Modified: 2013-01-22
I have a complex and sucessful query that that uses LISTAGG to show codes associated to a parent list.

My question is: can I use LISTAGG to produce and output a column with a specific number of delimiters.  I need to import this file into Excel for further processing, but the LISTAGG column needs 39 delimiters to process into 40 columns when imported into Excel regardless of the number of results.
0
Comment
Question by:rlinnabery
  • 3
  • 2
6 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
I'm not following what you are asking.

Are you asking how to produce a CSV with 40 columns no matter how many columns you have?

Are you wanting certain values in specific positions?

Can you post some sample data and expected results?
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
LISTAGG can't do that, but you can nest the query and use RPAD to create additional delimiters


SELECT owner, table_names || RPAD(',', 40 - cnt, ',')
  FROM (SELECT owner,
               COUNT(*) cnt,
               LISTAGG(table_name, ',') WITHIN GROUP (ORDER BY table_name) table_names
          FROM all_tables
         WHERE owner IN ('SCOTT', 'HR', 'OE', 'PM', 'SH')
        GROUP BY owner
        ORDER BY owner);
0
 

Author Closing Comment

by:rlinnabery
Comment Utility
Thanks you, sdstuber.  I think this will accomplish exactly what I needed.
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.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
Mind if I ask why you needed trailing commas to load into Excel?

Excel should easily handle:
a,b,c
a

and not need:
a,b,c
a,,
0
 

Author Comment

by:rlinnabery
Comment Utility
I am loading into a template that has twelve sections of up to 40 codes plus some other settings for the section.  I may have 1 or I may have 40 codes.  But I need them to take up the 40 cells when parsed into Excel so the next section starts in the proper cell for later input into a vba script after I make changes.  I used the listagg to minimize the columns in the query as it would be around 800 columns otherwise.  Hope that makes sense.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
Thanks for the post back.

I've never used templates.  Guess I can add that to my to-do list.
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.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to recover a database from a user managed backup

762 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

12 Experts available now in Live!

Get 1:1 Help Now