oracle 多行数据合并一行数据

在工作中遇见的oracle知识,多行合并成一行,记录一下

1.取出需要的数据,代码:

(SELECT to_char(m.f_meetdate, 'yyyy-MM-dd'),
        decode(nvl(m.f_meetsourceid, 0),0,'',1,'(评审会)',2,'(审保会)',3,'(公司风管会)',4,'(集团风管会)',5,'(集团董事会)',6,'(备份)'),
        m.f_meetingresult
 FROM tb_guar_meetrecord m
 where m.f_billid = 1600
      and m.f_billcode = 'period')

取出的数据:

2.现在想把这两行数据合并成一行,并且一行的多字段合并成一个字段;

代码:

(SELECT (to_char(m.f_meetdate, 'yyyy-MM-dd') || ' ' ||
         decode(nvl(m.f_meetsourceid, 0),0,'',1,'(评审会)',2,'(审保会)',3,'(公司风管会)',4,'(集团风管会)',5,'(集团董事会)',6,'(备份)') || ' ' ||
          m.f_meetingresult)
 FROM tb_guar_meetrecord m
 where m.f_billid = 1600
       and m.f_billcode = 'period')

取出的数据:

3.现在就要把这个两行合并,需要使用oracle wm_concat(column)函数实现;

代码:

(SELECT (replace(wm_concat(to_char(m.f_meetdate, 'yyyy-MM-dd') || ' ' ||
                 decode(nvl(m.f_meetsourceid, 0),0,'',1,'(评审会)',2,'(审保会)',3,'(公司风管会)',4,'(集团风管会)',5,'(集团董事会)',6,'(备份)') || ' ' ||
                 m.f_meetingresult),
                              ',',
                              '<br/>'))
  FROM tb_guar_meetrecord m
  where m.f_billid = 1600
        and m.f_billcode = 'period')

取出的数据:

这样就实现多行数据合并成一行了。

replace(wm_concat(name),',','<br/>')加上replace是把取出的数据中的‘,’换成我想要的‘<br/>’。

上面用到了decode函数,这个函数和if判断语句效果相同。

posted @ 2018-08-07 17:13  lvanka  阅读(34554)  评论(0编辑  收藏  举报