?
Solved

group by a string filed (concatenate the string field)

Posted on 2007-07-26
6
Medium Priority
?
2,452 Views
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

Thanks
0
Comment
Question by:Raju Srivatsavaye
5 Comments
 
LVL 9

Accepted Solution

by:
arioh earned 500 total points
ID: 19579945
0
 
LVL 14

Assisted Solution

by:sathyagiri
sathyagiri earned 500 total points
ID: 19580269
create or replace function fn_join
 (
     p_cursor sys_refcursor,
     p_delimiter varchar2 := ','
 ) return varchar2
 is
     l_value   varchar2(32767);
     l_result  varchar2(32767);
 begin
     loop
         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
0
 
LVL 27

Assisted Solution

by:sujith80
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
from
(
 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
/
0
 
LVL 14

Assisted Solution

by:GGuzdziol
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

http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2196162600402
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20228851
Forced accept.

Computer101
EE Admin
0

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