Home » SQL & PL/SQL » SQL & PL/SQL » Please solve my issue
Please solve my issue [message #624272] Sat, 20 September 2014 00:45 Go to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Please give me the solution

I have a table like this

SQL> craete table test(id number(3), Name varchar2(30));
Table created.

SQL> insert into test values(1, 'a');
1 row created.
SQL> insert into test values(1, 'b');
1 row created.
SQL> insert into test values(1, 'b');
1 row created.
SQL> insert into test values(2, 'd');
1 row created.
SQL> insert into test values(2, 'e');
1 row created.
SQL> insert into test values(3, 'f');
1 row created.
SQL> insert into test values(4, 'g');
1 row created.

SQL> Select id, name from test;

Id    Name
1       a
1       b
1       c
2       d
2       e
3       f
4       g


I want output like this

Id    Name
1      a, b, c
2      d, e
3      f
4      g
Re: Please solve my issue [message #624275 is a reply to message #624272] Sat, 20 September 2014 00:50 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Oracle version you use might be relevant; didn't you learn that, yet? For now, "LISTAGG" is the keyword you will be searching for.
Re: Please solve my issue [message #624278 is a reply to message #624275] Sat, 20 September 2014 01:15 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
I am using Oracle 10g

D:\>sqlplus/nolog

SQL*Plus: Release 10.2.0.3.0 - Production on Sat Sep 20 00:11:00 2014

Copyright (c) 1982, 2006, Oracle. All Rights Reserved.

[Updated on: Sat, 20 September 2014 01:16]

Report message to a moderator

Re: Please solve my issue [message #624279 is a reply to message #624278] Sat, 20 September 2014 01:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You have 2 usual ways: SYS_CONNECT_BY_PATH and T. Kyte's STRAGG function.
You can also use pipeline function and I bet there are solutions using XML functions (without speaking about WM_CONCAT undocumented one).

Re: Please solve my issue [message #624280 is a reply to message #624279] Sat, 20 September 2014 02:13 Go to previous messageGo to next message
jimit_shaili
Messages: 237
Registered: June 2006
Location: India, Ahmedabad
Senior Member
Dear Aditya,

Following will sovle your problem.

select id,listagg(name,',') within group(order by id) test_str from test group by id;

 ID TEST_STR
--- ----------
  1 a,b,c
  2 d,e
  3 f
  4 g



Regards

Jimit
Re: Please solve my issue [message #624281 is a reply to message #624280] Sat, 20 September 2014 02:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

@Jimit,

No, it will not, LISTAGG is a 11g function and OP is on 10g.

Re: Please solve my issue [message #624282 is a reply to message #624272] Sat, 20 September 2014 02:50 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
aaditya321 wrote on Sat, 20 September 2014 11:15

SQL> craete table test(id number(3), Name varchar2(30));
Table created.


/forum/fa/4921/0/
R.I.P. Oracle
Died on : Sat, 20 September 2014
Re: Please solve my issue [message #624283 is a reply to message #624281] Sat, 20 September 2014 03:18 Go to previous messageGo to next message
jimit_shaili
Messages: 237
Registered: June 2006
Location: India, Ahmedabad
Senior Member
Ah! my mistake. I didn't look at the version details of OP...
Re: Please solve my issue [message #624304 is a reply to message #624283] Sat, 20 September 2014 09:44 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Use below query

with tab as (select 1 as id , 'a' as name from dual union 
select 1 as id , 'b' as name from dual union 
select 1 as id , 'c' as name from dual union 
select 2 as id , 'e' as name from dual union 
select 2 as id , 'e' as name from dual union 
select 3 as id , 'f' as name from dual union 
select 4 as id , 'g' as name from dual )
select ID, wm_concat(name) name from tab GROUP BY ID;


Thanks,
Anil
Re: Please solve my issue [message #624305 is a reply to message #624304] Sat, 20 September 2014 09:48 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Anil,

Please post URL to official Oracle documentation where WM_CONCAT is documented.
Re: Please solve my issue [message #624306 is a reply to message #624305] Sat, 20 September 2014 10:27 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

WM_CONCAT is undocumented, I am using Oracle 11g R1, its working

SQL*Plus: Release 11.1.0.6.0 - Production on Sat Sep 20 20:55:09 2014

Copyright (c) 1982, 2007, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> desc wm_concat
FUNCTION wm_concat RETURNS VARCHAR2
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 P1                             VARCHAR2                IN

SQL>
Re: Please solve my issue [message #624307 is a reply to message #624306] Sat, 20 September 2014 10:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

1/ OP is on 10g
2/ The fact you can do a DESC does not prove it is documented. Post the URL of the documentation.
3/ Why are you posting something that has already be mentioned?

Re: Please solve my issue [message #624308 is a reply to message #624307] Sat, 20 September 2014 10:32 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

I have used wm_concat in Oracle 10g as well.

Re: Please solve my issue [message #624309 is a reply to message #624308] Sat, 20 September 2014 10:38 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
anil_mk wrote on Sat, 20 September 2014 21:02
I have used wm_concat in Oracle 10g as well.



If you did that in PRODUCTION, then immediately consult your manager and tell him honestly. And if some problem happens in production, then say this to yourself "R.I.P. my database". Since Oracle won't support at all.

Good luck!
Re: Please solve my issue [message #624310 is a reply to message #624309] Sat, 20 September 2014 10:48 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Thank you Lalit, I am not using 10g in production. Sorry all for undocumented function.

Re: Please solve my issue [message #624311 is a reply to message #624310] Sat, 20 September 2014 11:28 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Look, I am not trying to irritate you, but, my intention was to make you aware of the consequences.

If you have nothing to do with Production code, why do you waste time over undocumented features? That too when it was already told to you about it.
Re: Please solve my issue [message #624312 is a reply to message #624311] Sat, 20 September 2014 12:00 Go to previous messageGo to next message
anil_mk
Messages: 146
Registered: August 2006
Location: Bangalore, India
Senior Member

Lalit, I have commented first time on undocumented feature, yes it was my mistake.

Aaditya,
Please use below
with tab as (select 1 as id , 'a' as name from dual union 
select 1 as id , 'b' as name from dual union 
select 1 as id , 'c' as name from dual union 
select 2 as id , 'e' as name from dual union 
select 2 as id , 'e' as name from dual union 
select 3 as id , 'f' as name from dual union 
select 4 as id , 'g' as name from dual )
select  id,
        replace(ltrim(sys_connect_by_path(name,'|'),'|'),'|',', ') name
  from  (
         select  id,
                 name,
                 row_number() over(partition by id order by name) rn
           from  tab
        )
  where connect_by_isleaf = 1
  start with rn = 1
  connect by id = prior id
         and rn  = prior rn + 1
  order by id;

Id	Name
------------------------------------
1	a, b, c
2	e
3	f
4	g


[Updated on: Sat, 20 September 2014 12:03]

Report message to a moderator

Re: Please solve my issue [message #624314 is a reply to message #624312] Sat, 20 September 2014 12:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

It was Aaditya's work, we ALL know how to do it; why don't you let him find it by himself?
You think you helped him, you didn't.

Tell me and I'll forget; show me and I may remember; involve me and I'll understand.

[Updated on: Sat, 20 September 2014 12:22]

Report message to a moderator

Re: Please solve my issue [message #624337 is a reply to message #624314] Mon, 22 September 2014 02:18 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Thanks to all who is supporting me lot here.
Re: Please solve my issue [message #624431 is a reply to message #624308] Mon, 22 September 2014 13:06 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
anil_mk wrote on Sat, 20 September 2014 11:32
I have used wm_concat in Oracle 10g as well.


Sure you can use it, but you are in "Swim at your own risk - no lifeguard on site" situation.

"Lifeguard" (Oracle support) says:

Quote:
The function WMSYS.WM_CONCAT is an internal undocumented function which is installed/uninstalled as part of the Workspace Manager feature of Oracle Database. It is internally used in a number of Workspace Manager views. It is not meant to be used by customers directly, and could be changed/updated without notice by Oracle Development. Do not use the WMSYS.WM_CONCAT view in your application.


Read:

WMSYS.WM_CONCAT Should Not Be Used For Customer Applications, It Is An Internal Function [ID 1336219.1]

Problem with WMSYS.WM_CONCAT Function after Upgrading [ID 1300595.1]

SY.
Previous Topic: mutating trigger
Next Topic: Unable to drop a column in table.
Goto Forum:
  


Current Time: Fri Apr 26 04:20:28 CDT 2024