不能在同一个查询中使用 group by 和 over(partition by)?
Cannot use group by and over(partition by) in the same query?
我有一个包含 3 列的 table myTable
。 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
中的每个元素都有重复的值。第 3 列可以是任意值,如下所示:
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
我想要的是在 Value
列上使用聚合函数 SUM()
按 col_1
分区并按 col_2
分组。上面的 table 应该是这样的:
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.
没有太多经验
谢谢。
是的,你可以,但你应该在分组级别上保持一致。
也就是说,如果你的查询是一个 GROUP BY 查询,那么在一个解析函数中
您只能使用所选 "non-analytic" 部分中的 "detail" 列
列。
因此,您可以使用 GROUP BY 列或非分析聚合,
像这个例子:
select product_id, company,
sum(members) as No_of_Members,
sum(sum(members)) over(partition by company) as TotalMembership
From Product_Membership
Group by Product_ID, Company
希望对您有所帮助
SELECT col_1, col_2, sum(Value) over(partition by col_1) as sum_value
-- also try changing "col_1" to "col_2" in OVER
from myTable
GROUP BY col_2,col_1
我找到了解决方案。
我不需要使用 OVER(PARTITION BY col_1)
因为它已经在 GROUP BY
子句中。因此,以下查询给了我正确的答案:
SELECT col_1, col_2, sum(Value) as sum_value
from myTable GROUP BY col_1, col_2
因为我已经将 w.r.t col_1
和 col_2
.
分组
戴夫,谢谢,我从你的 post 那里得到了灵感。
我有一个包含 3 列的 table myTable
。 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
中的每个元素都有重复的值。第 3 列可以是任意值,如下所示:
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
我想要的是在 Value
列上使用聚合函数 SUM()
按 col_1
分区并按 col_2
分组。上面的 table 应该是这样的:
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.
没有太多经验谢谢。
是的,你可以,但你应该在分组级别上保持一致。 也就是说,如果你的查询是一个 GROUP BY 查询,那么在一个解析函数中 您只能使用所选 "non-analytic" 部分中的 "detail" 列 列。 因此,您可以使用 GROUP BY 列或非分析聚合, 像这个例子:
select product_id, company,
sum(members) as No_of_Members,
sum(sum(members)) over(partition by company) as TotalMembership
From Product_Membership
Group by Product_ID, Company
希望对您有所帮助
SELECT col_1, col_2, sum(Value) over(partition by col_1) as sum_value
-- also try changing "col_1" to "col_2" in OVER
from myTable
GROUP BY col_2,col_1
我找到了解决方案。
我不需要使用 OVER(PARTITION BY col_1)
因为它已经在 GROUP BY
子句中。因此,以下查询给了我正确的答案:
SELECT col_1, col_2, sum(Value) as sum_value
from myTable GROUP BY col_1, col_2
因为我已经将 w.r.t col_1
和 col_2
.
戴夫,谢谢,我从你的 post 那里得到了灵感。