Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Query using COMMA SEPERATED LIST of a Query as COLUMNS NAMES

user5743038Aug 2 2017 — edited Aug 4 2017

Hi,

Oracle Gurus!

I have a requirement for a SQL solution and only a SQL Solution or the approach listed below is even Possible, or if any one can give me a Better Solution

Lets Say I have a Scenario

Where in I Have a Query with Comma separated List as Data using LISTAGG() Function.

Now I have a Select query with a Comma Separated list as output, And These Comma Separated Lists are defined as Column names in another Table(say Table name form_item_nm),

the Task is to write a Query which will Pass this Comma separated Values in the Query as Column names from the form_item_nm table

I have Two Tables

with Structures as

Table: ITEM_DEF_TABLE

idColumnVal
1item_nameitem1
2item_nameitem2
3item_nameitem3
5item_nameitem4

Table: FORM_ITEM_NM

id item1 item2item3item4
1ABC1DEF1XYZ1ABB1
2ABC2DEF2XYZ2ABB2
3ABC3DEF3XYZ3ABB3
4ABC4DEF4XYZ4ABB4

Please find the Example

Query1:

SELECT LISTAGG(item_name,',') WITHIN GROUP (ORDER BY itme_name) item_name_list

FROM item_def_table

The Output is (item1,item2,item3,item4)

THEN

QUERY 2

SELECT

(SELECT LISTAGG(item_name,',') WITHIN GROUP (ORDER BY itme_name) item_name_list

FROM item_def_table)

FROM form_item_nm;

But Unfortunately I am getting only Result set as the Same List returened with Number of Rows in my  form_item_nm table

i.e

Lets Say I got 4 Rows in my form_item_nm I am getting the Result of my Query2 is like this below

SELECT (SELECT LISTAGG(item_name,',') WITHIN GROUP (ORDER BY itme_name) item_name_list FROM item_def_table) FROM form_item_nm;

item1,item2,item3,item4
item1,item2,item3,item4
item1,item2,item3,item4
item1,item2,item3,item4
item1,item2,item3,item4

Thanks

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 1 2017
Added on Aug 2 2017
9 comments
49,035 views