加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle – LISTAGG功能与两列

发布时间:2020-12-12 15:12:59 所属栏目:百科 来源:网络整理
导读:我有这样一张桌子(报告) --------------------------------------------------| user_id | Department | Position | Record_id |--------------------------------------------------| 1 | Science | Professor | 1001 || 1 | Maths | | 1002 || 1 | History
我有这样一张桌子(报告)
--------------------------------------------------
|  user_id |  Department | Position  | Record_id |
--------------------------------------------------
|  1       |  Science    | Professor |  1001     |
|  1       |  Maths      |           |  1002     |
|  1       |  History    | Teacher   |  1003     |
|  2       |  Science    | Professor |  1004     |
|  2       |  Chemistry  | Assistant |  1005     |
--------------------------------------------------

我想要得到以下结果

---------------------------------------------------------
   | user_id  |  Department+Position                       |
   ---------------------------------------------------------
   |  1       | Science,Professor;Maths,; History,Teacher |
   |  2       | Science,Professor; Chemistry,Assistant   |
   ---------------------------------------------------------

这意味着我需要保留空格,就像在结果表中看到的那样.
现在我知道如何使用LISTAGG函数,但只有一列.但是,我不知道在同一时间怎么做两列.这是我的查询:

SELECT user_id,LISTAGG(department,';') WITHIN GROUP (ORDER BY record_id)
FROM report

提前致谢 :-)

在集合中只需要明智地使用连接:
select user_id,listagg(department || ',' || nvl(position,' '),'; ')
        within group ( order by record_id )
  from report

即通过逗号和位置聚合部门的连接,如果为空,则将其替换为空格.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读