我有一個表格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
中的每個元素具有重複值。第三列可以有任意值,如下所示:不能在同一個查詢中一次又一次使用group(partition by)?
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
我想是由col_1
在Value
列分區使用聚合功能SUM()
和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經驗。
謝謝。
MySQL有什麼問題? (不要標記不涉及的產品...) – jarlh 2015-03-25 08:17:55