Another Way to Solve Last Problem.

http://www.oracle.com/technology/oramag/code/tips2004/050304.html


1. create a function to concatenate strings.

CREATE OR REPLACE FUNCTION rowtocol( 
       p_slct IN VARCHAR2,
       p_dlmtr IN VARCHAR2 DEFAULT ',' 
) RETURN VARCHAR2 

  AUTHID CURRENT_USER AS

TYPE c_refcur IS REF CURSOR;

     lc_str VARCHAR2(4000);
     lc_colval VARCHAR2(4000);
     c_dummy c_refcur;

     BEGIN

     OPEN c_dummy FOR p_slct;
     LOOP
       FETCH c_dummy INTO lc_colval;
       EXIT WHEN c_dummy%NOTFOUND;
       lc_str := lc_str || p_dlmtr || lc_colval;
     END LOOP;

     CLOSE c_dummy;
     RETURN SUBSTR(lc_str,2);

     /* 
     EXCEPTION 
     WHEN OTHERS THEN
       lc_str := SQLERRM;
  
       IF c_dummy%ISOPEN THEN
          CLOSE c_dummy;
       END IF;
  
       RETURN lc_str;
     */
    END;


 Usage :

select distinct t.author, 
          rowtocol('select book from table_name where author=''' || t.author|| '''')
from table_name t
;


 or


select t.author, rowtocol('select book from table_name where author=''' || t.author|| '''', '#')
from table_name t
group by t.author
;

 


cons: less natural than user-defined aggregate function 

pros: more flexible, aggregate function could take only one parameter

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值