ClickHouse exception, code: 215, host: 123.249.3.11, port: 8123; Code: 215, e.displayText() = DB::Exception: Column `sales` is not under aggregate function and not in GROUP BY: While processing period_code, formatDateTime(parseDateTimeBestEffort(period_start), '%Y') AS years, product_name AS product, sum(sales), sum(sales) OVER (PARTITION BY years ORDER BY period_code DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS sale (version 21.3.4.25)
时间: 2024-02-27 13:54:57 浏览: 241
ClickHouse 是俄罗斯的 Yandex 于 2016 年开源的用于在线分析处理查询
这个异常提示指出了问题所在:`Column 'sales' is not under aggregate function and not in GROUP BY`。这意味着你的查询中包含了未被聚合函数或 GROUP BY 子句包含的列。
同时,你在查询中也使用了窗口函数 `sum(sales) OVER (PARTITION BY years ORDER BY period_code DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS sale`,这可能会导致 ClickHouse 引擎无法正确处理这个查询。
要解决这个问题,你需要将所有未被聚合函数或 GROUP BY 子句包含的列都包含在窗口函数中,或者将窗口函数和聚合函数分别使用。以下是一个修改后的示例:
```
SELECT formatDateTime(parseDateTimeBestEffort(period_start), '%Y') AS years,
product_name AS product,
sum(sales) AS total_sales,
sum(sales) OVER (PARTITION BY formatDateTime(parseDateTimeBestEffort(period_start), '%Y') ORDER BY period_code DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total_sales
FROM your_table
GROUP BY years, product;
```
在这个查询中,我们将 `formatDateTime(parseDateTimeBestEffort(period_start), '%Y')` 列包含在了窗口函数中,并使用了 GROUP BY 子句。这样,你就可以正确地计算每个产品每年的销售总额和累计销售总额了。
阅读全文