LISTAGG question

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.
rlinnaberyAsked:
Who is Participating?
 
sdstuberConnect With a Mentor Commented:
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
 
slightwv (䄆 Netminder) Commented:
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
 
rlinnaberyAuthor Commented:
Thanks you, sdstuber.  I think this will accomplish exactly what I needed.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
slightwv (䄆 Netminder) Commented:
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
 
rlinnaberyAuthor Commented:
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
 
slightwv (䄆 Netminder) Commented:
Thanks for the post back.

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

From novice to tech pro — start learning today.