Solved

LISTAGG question

Posted on 2013-01-17
6
653 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
[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
  • 3
  • 2
6 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38790133
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 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 38790247
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
ID: 38793596
Thanks you, sdstuber.  I think this will accomplish exactly what I needed.
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 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38793637
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
ID: 38799621
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 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38805491
Thanks for the post back.

I've never used templates.  Guess I can add that to my to-do list.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ODBC in excel 2016 in Windows 10 via VBA 16 376
pl/sql parameter is null sometimes 2 36
SQL Syntax Question 9 52
Automate an Oracle update in Excel 7 61
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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Via a live example, show how to take different types of Oracle backups using RMAN.

740 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