Hive 在分区上嵌套 SUM - 错误表达式不在 GROUP BY 键中
Posted
技术标签:
【中文标题】Hive 在分区上嵌套 SUM - 错误表达式不在 GROUP BY 键中【英文标题】:Hive nested SUM over partition - error Expression not in GROUP BY key 【发布时间】:2021-05-07 01:51:22 【问题描述】:我试图在一个查询中获得累积总和。它在 SQL、PRESTO 等中运行良好,但在 HIVE 中却不行,这会引发错误消息。
create table test(store varchar(10), item int, quantity int)
insert into test
select 'depot',101,1
union select 'depot',101,2
union select 'depot',101,5
union select 'depot',102,1
union select 'depot',102,3
store | item | revenue |
---|---|---|
depot | 101 | 1 |
depot | 101 | 2 |
depot | 101 | 5 |
depot | 102 | 1 |
depot | 102 | 3 |
select store, item,
sum(sum(revenue)) over (partition by store order by item)
from test
group by store, item
预期输出:
store | item | quantity |
---|---|---|
depot | 101 | 8 |
depot | 102 | 12 |
错误:
[代码:40000,SQL 状态:42000] 编译语句时出错: 失败:SemanticException 无法将窗口调用分解为 团体。至少 1 个组必须仅依赖于输入列。还要检查 对于循环依赖。潜在错误: org.apache.hadoop.hive.ql.parse.SemanticException:第 1:24 行 表达式不在 GROUP BY 键“收入”中
有什么建议吗?
【问题讨论】:
【参考方案1】:您可以将查询分开:
select store, item, sum(sum_revenue) over (partition by store order by item) as revenue
from (
select store, item, sum(revenue) as sum_revenue
from test
group by store, item
) as t
【讨论】:
以上是关于Hive 在分区上嵌套 SUM - 错误表达式不在 GROUP BY 键中的主要内容,如果未能解决你的问题,请参考以下文章