Solved

Oracle query grouping questions

Posted on 2011-03-03
5
436 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 76

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 76

Accepted Solution

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

Assisted Solution

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

777 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