【问题标题】:SQL - Select Join and GroupSQL - 选择加入和组
【发布时间】:2013-04-25 07:56:49
【问题描述】:

连接两个表的SELECT语句的结果如下-

Col 1   |  Col 2    |  Col 3   |  Col 4
 ----------------------------------
sample11| sample12  | sample13 | sample14
sample11| sample12  | sample23 | sample24
sample11| sample12  | sample23 | sample34
sample11| sample12  | sample43 | sample44

如何将第 4 列中的值对第 2 行和第 3 行进行分组和连接(基于第 3 列中的不同值),以使结果如下 -

Col 1   |  Col 2    |  Col 3   |  Col 4
 ----------------------------------
sample11| sample12  | sample13 | sample14
sample11| sample12  | sample23 | sample24, sample34
sample11| sample12  | sample43 | sample44

【问题讨论】:

    标签: sql oracle join group-by concatenation


    【解决方案1】:

    如果您使用的是 oracle 11g

    SELECT  "Col 1", "Col 2", "Col 3", 
            LISTAGG("Col 4", ',') WITHIN GROUP (ORDER BY "Col 1") AS "Col 4"
    FROM    TableName
    GROUP   BY "Col 1", "Col 2", "Col 3"
    

    【讨论】:

      猜你喜欢
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多