Solved

Oracle query grouping questions

Posted on 2011-03-03
5
438 Views
Last Modified: 2012-05-11
I have an oracle 11g table
tab1
----
f1      f2
A      p
A      q
B      r
C     m
A      s
B      g
C     x

How do I get this output with best performance? I need the query to run on a table that has 100K records.
O/P:
----
F1   F2
-----------
A     p,q,s
B     r,g
C     m,x
0
Comment
Question by:toooki
  • 3
5 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35032810
Try this

SELECT
       f1,
       RTRIM(
           EXTRACT(XMLAGG(XMLELEMENT("s", tab1.f2 || ',')),
'/s/text()').getstringval(),
           ','
       )
           f2
FROM tab1
GROUP BY f1
/
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 338 total points
ID: 35032820
If 11gR2, there is a LISTAGG function that is probably better.
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 137 total points
ID: 35032919
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35033077
>>for all possible
I'm on mobile and can't fully read that link but it I could not find the XML method posted above.

With the exception of listagg, also mentioned above, the others are a little out-dated.

If you don't have 11gR2 and can use listagg, the XML method almost always out-performs the others listed in that link.
0
 

Author Comment

by:toooki
ID: 35038730
Many thanks.The LISTAGG function worked perfectly for me..
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

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

726 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