存储过程系列二:适用函数wm_concat(column)函数实现字段合并

1、学习wm_concat函数

oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并

shopping:

-----------------------------------------

u_id       goods            num

------------------------------------------

1                苹果                2

2                 梨子               5

1                 西瓜               4

3                 葡萄               1

3                香蕉                1

1               橘子                 3

=======================

想要的结果为:

--------------------------------

u_id          goods_sum

____________________

1              苹果,西瓜,橘子

2              梨子

3              葡萄,香蕉

---------------------------------

  1. select u_id, wmsys.wm_concat(goods) goods_sum  
  2.  
  3. from shopping  
  4.  
  5. group by u_id  

想要的结果2:

--------------------------------

u_id          goods_sum

____________________

1              苹果(2斤),西瓜(4斤),橘子(3斤)

2              梨子(5斤)

3              葡萄(1斤),香蕉(1斤)

---------------------------------

使用oracle wm_concat(column)函数实现:

  1. select u_id, wmsys.wm_concat(goods || '(' || num || '斤)' ) goods_sum  
  2.  
  3. from shopping  
  4.  
  5. group by u_id  
  6.  

存储过程中使用到的函数

--  生产地址
  -- 通过转码表拼接生产地址信息
 --  update GG_XKZ_YLQXSCXKESL set   QYSCDZ =
 --  (   select wm_concat((select p.name from pub_codetable p where p.code = scdz.szqx and p.codetable='qxdm') ||
 --       (select p.name from pub_codetable p where p.code = scdz.szjd and p.codetable='jddm') ||
 --        scdz.jtdz) from XZXK_YLQXSCXKESL_HF_SQ sq, Xzxk_Ylqxscxkesl_Hf_Scdz scdz
 --       where sq.sqid = scdz.sqid and sq.sqid = sqidIn )
 --  where sqid = sqidIn;

  update GG_XKZ_YLQXSCXKESL set  QYSCDZ =
  (   select  wm_concat( scdz.jtdz )  from XZXK_YLQXSCXKESL_HF_SQ sq, Xzxk_Ylqxscxkesl_Hf_Scdz scdz
        where sq.sqid = scdz.sqid and sq.sqid = sqidIn )
   where sqid = sqidIn;

oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并,如果您对oracle wm_concat(column)函数使用方面感兴趣的话,不妨一看。

原文地址:https://www.cnblogs.com/meimao5211/p/4118680.html