XMLAGG Function [message #616994] |
Tue, 24 June 2014 01:30 |
|
vicktorz
Messages: 26 Registered: March 2013
|
Junior Member |
|
|
Hi All,
Your help is appreciated here..
I have a table with multiple rows of data.. i want to print all rows values in single column enclosed with single quotes..
Example:
table Domain
Domain_name
***********
vs
LS
RS
AS
Output should be like= 'vs','LS','RS','AS'
hOW do this operation using XMLAGG function..
I'm new to pl/SQL, Kidnly guide me..
|
|
|
Re: XMLAGG Function [message #616996 is a reply to message #616994] |
Tue, 24 June 2014 01:52 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
If XMLAGG is not mandatory to use, then LISTAGG would do the work too :
SQL> WITH DATA AS(
2 SELECT 'vs' str FROM dual UNION ALL
3 SELECT 'LS' FROM dual UNION ALL
4 SELECT 'RS' FROM dual UNION ALL
5 SELECT 'AS' FROM dual)
6 SELECT ''''||listagg(str,''',''') WITHIN GROUP(ORDER BY rn)||'''' str FROM
7 (SELECT ROWNUM rn, str FROM DATA);
STR
--------------------------------------------------------------------------------
'vs','LS','RS','AS'
Regards,
Lalit
|
|
|
|
Re: XMLAGG Function [message #616999 is a reply to message #616997] |
Tue, 24 June 2014 02:06 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Lalit's query does not depend on the number of rows and values in your table.
Quote:In your query, manually selecting values and unioning everytime will not be feasiable..
He had to do this because you didn't provide a test case.
You have to realize we have not your table and data, so we have to simulate it, this is the purpose of WITH clause here.
[Updated on: Tue, 24 June 2014 02:08] Report message to a moderator
|
|
|
Re: XMLAGG Function [message #617000 is a reply to message #616997] |
Tue, 24 June 2014 02:08 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
vicktorz wrote on Tue, 24 June 2014 12:32But here, values VS,LS,RS,AS are sample data only.. it may change in future upto infinite values in the table..
so i need to write query dynamically..
In your query, manually selecting values and unioning everytime will not be feasiable
LOL...The WITH clause is just like your table. Since you did not provide any test case, I had to do it manually.
All you need to do is, remove the WITH clause and just keep the same query and replace DATA with your table_name.
SELECT ''''||listagg(str,''',''') WITHIN GROUP(ORDER BY rn)||'''' str
FROM
(SELECT ROWNUM rn, str FROM <your_table_name>);
Makes sense?
Regards,
Lalit
|
|
|
|
|
|
|
|
Re: XMLAGG Function [message #617034 is a reply to message #617033] |
Tue, 24 June 2014 05:05 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
vicktorz wrote on Tue, 24 June 2014 15:29Great;)
Thank you Michel:)
But Michel did not use varying in list at all(he helped you to understand) and your code was to look for a value in a set of values(rather a list of values concatenated as a string). So what were you tring to achieve?
|
|
|