hive--grouping sets相关报错

1.missing ) at ‘,’ near ‘)’,同时其报错位置指向grouping sets 括号内部

  • 报错sql
select  tab1.a
       ,tab1.b
       ,sum(tab1.c)
from tab1
group by  tab1.a
         ,tab1.b
grouping sets (
    (tab1.a, tab1.b)
    )
;
  • 报错信息
ParseException line 7:22 missing ) at ',' near '<EOF>'
line 7:31 extraneous input ')' expecting EOF near '<EOF>'
  • 解决
select  tab1.a
       ,tab1.b
       ,sum(tab1.c)
from tab1
group by  tab1.a
         ,tab1.b
grouping sets ((a, tab1.b))
;
  • 原因
例如:
表tab1 有a,b,c 三列
表tab2 有a,d两列
就不能写成如下形式
select  tab1.a
       ,b
       ,d 
       ,sum(tab1.c)
from tab1
join tab2
on tab1.a = tab2.b
group by  tab1.a
         ,b
         ,d
grouping sets ((tab1.a, b, d))
;

应该改为:
select  tab1.a
       ,b
       ,d 
       ,sum(tab1.c)
from tab1
join tab2
on tab1.a = tab2.b
group by  tab1.a
         ,b
         ,d
grouping sets ((b, tab1.a, d))
;

即不能使tab1.a 放在第一位置,但如果只有tab1.a的话是可以的

如下是正确的
select  tab1.a
       ,b
       ,d 
       ,sum(tab1.c)
from tab1
join tab2
on tab1.a = tab2.b
group by  tab1.a
         ,b
         ,d
grouping sets ((tab1.a))
;

2.SemanticException 104:1 [Error 10213]: Grouping sets expression is not in GROUP BY key. Error encountered near token …

  • group by中字段与Grouping sets中字段,两者必须保持完全一致

3. FAILED: SemanticException [Error 10210]: Grouping sets aggregations (with rollups or cubes) are not allowed if aggregation function parameters overlap with the aggregation functions columns

  • 该错误是指cube中的维度不能参数聚合函数的运算
    • 例如 sum中拿cube维度做了判断
select a
	,b
	,c
	,sum(d) -- 不报错
	,sum(case when a=1 then d end) -- 会报错
from table
group a,b,c
grouping sets((a,b), (a,b,c))
  • 但是在spark-sql中是可以通过的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值