Home » SQL & PL/SQL » SQL & PL/SQL » string aggregation (oracle 11g)
string aggregation [message #637686] Thu, 21 May 2015 12:10 Go to next message
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 Go to previous messageGo to next message
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 Go to previous message
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:50
With 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.
...


Previous Topic: date Format error
Next Topic: Select max date in row of columns. . .
Goto Forum:
  


Current Time: Thu Apr 25 05:05:59 CDT 2024