【问题标题】:How to aggregrate multiple rows with more than one columns in Oracle?如何在Oracle中聚合多行多列?
【发布时间】:2017-07-26 12:38:35
【问题描述】:

我有一个包含三列的表(名为“游戏”):天气、体育和客户

weather sports       customer
sun     volleyball   Randy 
sun     volleyball   Lau
sun     gym          Ryan
sun     gym          Rachel

表格

weather sports       customer
sun     volleyball   Randy, Lau
sun     gym          Ryan, Rachel

我使用了以下 LISTAGG 命令,但它给了我错误提示“不是按表达式分组”

SELECT
    weather, sports,
    LISTAGG(customer, ',') WITHIN GROUP (ORDER BY sports) "Customer"
    FROM games
    GROUP BY customer;

【问题讨论】:

    标签: sql database listagg


    【解决方案1】:

    GROUP BY 需要包含 未聚合的 列。他们定义结果集中的每一行:

    SELECT weather, sports,
           LISTAGG(customer, ',') WITHIN GROUP (ORDER BY sports) as Customers
    FROM games
    GROUP BY weather, sports;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多