oracle行列转换-多行转换成字符串

建表语句及初始化数据脚本如下:

点击(此处)折叠或打开

  1. SQL> create table t_row_str(
  2.   2 id int,
  3.   3 col varchar2(10));

  4. 表已创建。

  5. SQL> insert into t_row_str values(1,'a');

  6. 已创建 1 行。

  7. SQL> insert into t_row_str values(1,'b');

  8. 已创建 1 行。

  9. SQL> insert into t_row_str values(1,'c');

  10. 已创建 1 行。

  11. SQL> insert into t_row_str values(2,'a');

  12. 已创建 1 行。

  13. SQL> insert into t_row_str values(2,'d');

  14. 已创建 1 行。

  15. SQL> insert into t_row_str values(2,'e');

  16. 已创建 1 行。

  17. SQL> insert into t_row_str values(3,'c');

  18. 已创建 1 行。

  19. SQL> commit;

  20. 提交完成。

  21. SQL> select * from t_row_str;

  22.         ID COL
  23. ---------- ----------

  24.          1 a
  25.          1 b
  26.          1 c
  27.          2 a
  28.          2 d
  29.          2 e
  30.          3 c

  31. 已选择7行。
多行转换成字符串有以下多种方式
1)MAX + decode
适用范围:8i,9i,10g及以后版本

点击(此处)折叠或打开

  1. SQL> select id,max(decode(rn,1,col,null))||
  2.   2 max(decode(rn,2,','||col,null))||
  3.   3 max(decode(rn,3,','||col,null)) str
  4.   4 from(select id,col,row_number() over(partition by id order by col) as rn
  5.   5 from t_row_str) t
  6.   6 group by id
  7.   7 order by 1;

  8.         ID STR
  9. ---------- --------------------------------

  10.          1 a,b,c
  11.          2 a,d,e
  12.          3 c
2) row_number + lead(适用范围:8i,9i,10g及以后版本)

点击(此处)折叠或打开

  1. SQL> select id,str from (select id,row_number() over(partition by id order by col) as
  2.   2 rn,
  3.   3 col||lead(','||col,1) over(partition by id order by col)||lead(','||col,2) over(partition by id
  4.  order by col) as str
  5.   4 from t_row_str)
  6.   5 where rn=1
  7.   6 order by 1
  8.   7 /

  9.         ID STR
  10. ---------- --------------------------------

  11.          1 a,b,c
  12.          2 a,d,e
  13.          3 c
注:oracle的lead函数是偏移量函数,其用途是访问某一字段的下一个值(lag函数用法一样,只是访问上一个值)。如下代码所示:

点击(此处)折叠或打开

  1. SQL> select id,col,lead(col,1) over(partition by id order by col) lead_1,
  2.   2 lead(col,2) over(partition by id order by col) lead_2,
  3.   3 lead(col,3) over(partition by id order by col) lead_3
  4.   4 from t_row_str;

  5.         ID COL LEAD_1 LEAD_2 LEAD_3
  6. ---------- ---------- ---------- ---------- ----------

  7.          1 a b c
  8.          1 b c
  9.          1 c
  10.          2 a d e
  11.          2 d e
  12.          2 e
  13.          3 c

  14. 已选择7行。

3)MODEL
适用范围:10g及以后版本

点击(此处)折叠或打开

  1. SQL> SELECT id, substr(str, 2) str FROM t_row_str
  2.   2 MODEL
  3.   3 RETURN UPDATED ROWS
  4.   4 PARTITION BY(ID)
  5.   5 DIMENSION BY(row_number() over(PARTITION BY ID ORDER BY col) AS rn)
  6.   6 MEASURES (CAST(col AS VARCHAR2(20)) AS str)
  7.   7 RULES UPSERT
  8.   8 ITERATE(3) UNTIL( presentv(str[iteration_number+2],1,0)=0)
  9.   9 (str[0] = str[0] || ',' || str[iteration_number+1])
  10.  10 ORDER BY 1;

  11.         ID STR
  12. ---------- --------------------------------------

  13.          1 a,b,c
  14.          2 a,d,e
  15.          3 c

4)sys_connect_by_path
适用范围:8i,9i,10g及以后版本

点击(此处)折叠或打开

  1. SQL> edit;
  2. 已写入 file afiedt.buf

  3.   1 SELECT t.id id, MAX(substr(sys_connect_by_path(t.col, ','), 2)) str
  4.   2 FROM (SELECT id, col, row_number() over(PARTITION BY id ORDER BY col) rn
  5.   3 FROM t_row_str) t
  6.   4 START WITH rn = 1
  7.   5 CONNECT BY rn = PRIOR rn + 1
  8.   6 AND id = PRIOR id
  9.   7* GROUP BY t.id
  10. SQL> /

  11.        ID STR
  12. ---------- --------------------------------------

  13.          1 a,b,c
  14.          2 a,d,e
  15.          3 c
  16. 以下代码适用于10G及之后的版本
  17. SQL> edit
  18. 已写入 file afiedt.buf

  19.   1 SELECT t.id id, substr(sys_connect_by_path(t.col, ','), 2) str
  20.   2 FROM (SELECT id, col, row_number() over(PARTITION BY id ORDER BY col) rn
  21.   3 FROM t_row_str) t
  22.   4 where connect_by_isleaf=1
  23.   5 START WITH rn = 1
  24.   6 CONNECT BY rn = PRIOR rn + 1
  25.   7* AND id = PRIOR id
  26. SQL> /

  27.        ID STR
  28. ---------- --------------------------------------

  29.          1 a,b,c
  30.          2 a,d,e
  31.          3 c
注:sys_connect_by_path函数使用之前,必须先构造出一个树,否则无法使用,上例用row_number函数构造树型结构。
5)wmsys.wm_concat
适用范围:10g及以后版本
这个函数预定义按','分隔字符串,若要用其他符号分隔可以用,replace将','替换。

点击(此处)折叠或打开

  1. SQL> select id,wmsys.wm_concat(col) from t_row_str
  2.   2 group by id;

  3.         ID
  4. ----------

  5. WMSYS.WM_CONCAT(COL)
  6. -------------------------------------------------------

  7.          1
  8. a,b,c

  9.          2
  10. a,d,e

  11.          3
  12. c




来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/21251711/viewspace-1066878/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/21251711/viewspace-1066878/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值