我有一个myTable有 3 列的表。col_1是一个INTEGER,其他 2 列是DOUBLE。例如,col_1={1, 2}, col_2={0.1, 0.2, 0.3}。中的每个元素col_1都由 的所有值组成,col_2并且col_2对于 中的每个元素都有重复的值col_1。第三列可以有任何值,如下所示:
col_1 | col_2 | Value
----------------------
1 | 0.1 | 1.0
1 | 0.2 | 2.0
1 | 0.2 | 3.0
1 | 0.3 | 4.0
1 | 0.3 | 5.0
2 | 0.1 | 6.0
2 | 0.1 | 7.0
2 | 0.1 | 8.0
2 | 0.2 | 9.0
2 | 0.3 | 10.0
我想要的是SUM()在Value列分区上使用聚合函数col_1并按col_2. 上表应如下所示:
col_1 | col_2 | sum_value
----------------------
1 | 0.1 | 1.0
1 | 0.2 | 5.0
1 | 0.3 | 9.0
2 | 0.1 | 21.0
2 | 0.2 | 9.0
2 | 0.3 | 10.0
我尝试了以下 SQL 查询:
SELECT col_1, col_2, sum(Value) over(partition by col_1) as sum_value
from myTable
GROUP BY col_1, col_2
但在 DB2 v10.5 上,它给出了以下错误:
SQL0119N An expression starting with "Value" specified in a SELECT
clause, HAVING clause, or ORDER BY clause is not specified in the
GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER
BY clause with a column function and no GROUP BY clause is specified.
你能指出什么是错的。我对 SQL 没有太多经验。
谢谢你。