group by a string filed (concatenate the string field)

Posted on 2007-07-26
Medium Priority
Last Modified: 2012-05-05
Here is my PL/SQL query

select tbl1.col1, tbl2.col2, tbl3.col3
from tbl1, tbl2, tbl3
where tbl1.col1 = tbl2.col1
and tbl2.col2 = tbl3.col2
group by
tbl1.col1, tbl2.col2, tbl3.col3

here is the output

col1             col2                 col3
1                 123                  val1
1                 123                  val2
2                 234                  val3
2                 234                  val4
2                 234                  val5

i want to see the output as

col1             col2                 col3
1                  123                 val1-val2
2                  234                 val3-val4-val5

in other words i want to concatenate the col3 field

hope i was clear with the description..... please help me out

Question by:Raju Srivatsavaye

Accepted Solution

arioh earned 500 total points
ID: 19579945
LVL 14

Assisted Solution

sathyagiri earned 500 total points
ID: 19580269
create or replace function fn_join
     p_cursor sys_refcursor,
     p_delimiter varchar2 := ','
 ) return varchar2
     l_value   varchar2(32767);
     l_result  varchar2(32767);
         fetch p_cursor into l_value;
         exit when p_cursor%notfound;
         if l_result is not null then
             l_result := l_result || p_delimiter;
         end if;
         l_result := l_result || l_value;
     end loop;
     return l_result;
 end fn_join;

Then use

select tbl1.col1, tbl2.col2, fn_join(cursor (select tbl3.col3
from tbl3 where tbl1.col1 = tbl2.col1
and tbl2.col2 = tbl3.col2)) from tbl1,tbl2
LVL 27

Assisted Solution

sujith80 earned 500 total points
ID: 19580585
select col1, col2, trim('-' from sys_connect_by_path(col3, '-') )
from (
select col1, col2, col3,
row_number() over(partition by col1, col2 order by col3) rn,
count(*) over(partition by col1, col2) cp
 select tbl1.col1, tbl2.col2, tbl3.col3
 from tbl1, tbl2, tbl3
 where tbl1.col1 = tbl2.col1
 and tbl2.col2 = tbl3.col2
where rn = cp
start with rn = 1
connect by prior col1 = col1
and prior col2 = col2
and prior rn = rn - 1
LVL 14

Assisted Solution

GGuzdziol earned 500 total points
ID: 19590937
It depends on Oracle version You have. In 8i there's no sys_connect_by_path I believe, moreover You cannot define Your own aggregate. Here are some links that describe methods for different dbms's


Expert Comment

ID: 20228851
Forced accept.

EE Admin

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

862 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