string aggregation [message #637686] |
Thu, 21 May 2015 12:10 |
patneel
Messages: 47 Registered: June 2007 Location: India
|
Member |
|
|
Hi All
I have data stored as shown below in table and need to get the concatenated output
I tried out following but not getting desired output.. any suggestions?
SELECT
ccy.id,
regexp_replace((SELECT listagg(column_value,'; ') within GROUP (ORDER BY column_value) FROM TABLE(ccy.ccys)),'([^;]+)(;\1)+','\1') AS Currency,
regexp_replace((SELECT listagg(column_value,'; ') within GROUP (ORDER BY column_value) FROM TABLE(ccy.linenum)),'([^;]+)(;\1)+','\1') AS Linenum
from (SELECT mandate_id, COLLECT(line_num)linenum,COLLECT(currency_id) ccys
FROM t1 WHERE active='Y'
GROUP BY id)ccy
where ccy.id='746'
Table data
ID Line Num Currency
746 1 CHN
746 1 JPY
746 1 USD
746 2 INR
746 3 GBP
746 3 HKD
746 5 CHN
746 5 JPY
746 5 USD
Output
ID LINE NUM CURRENCY
746 1;5 CHN;JPY;USD
746 2 INR
746 3 GBP;HKD
|
|
|
Re: string aggregation [message #637691 is a reply to message #637686] |
Thu, 21 May 2015 12:35 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
SQL> select *
2 from tbl
3 /
ID LINE_NUM CURRENCY
---------- -------- ----------
746 1 CHN
746 1 JPY
746 1 USD
746 2 INR
746 3 GBP
746 3 HKD
746 5 CHN
746 5 JPY
746 5 USD
9 rows selected.
SQL> column line_num format a10
SQL> column currency format a11
SQL> with t as (
2 select id,
3 line_num,
4 listagg(currency,',') within group(order by currency) currency
5 from tbl
6 group by id,
7 line_num
8 )
9 select id,
10 listagg(line_num,',') within group(order by line_num) line_num,
11 currency
12 from t
13 group by id,
14 currency
15 order by id,
16 line_num
17 /
ID LINE_NUM CURRENCY
---------- ---------- -----------
746 1,5 CHN,JPY,USD
746 2 INR
746 3 GBP,HKD
SQL>
SY.
|
|
|
Re: string aggregation [message #637693 is a reply to message #637686] |
Thu, 21 May 2015 12:47 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Don't forget:
Michel Cadot wrote on Mon, 23 July 2012 07:50With any SQL question, Post a working Test case: create table and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.
...
|
|
|