Solved

Oracle query grouping questions

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Oracle Finace 3 45
Oracle - How to analyze data using DATE COLUMN? 7 60
Schema creation in Oracle12c 6 23
sql query 9 19
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now