HiveSQL-错误“预期”输入不匹配

时间:2020-06-26 10:36:16

标签: sql input hiveql

我有一些这样的代码。 我想按分区(月)和数据使用量(g.volumn)来计算电话号码(isdn)的数量

select partition,
    case when a.g_volume = 0 then '0MB'
         when a.g_volume <=10 and a.g_volume > 0 then '0-10MB'
         when a.g_volume <=50 and a.g_volume >10 then '10 - 50MB'
    end as data,
    count(distinct a.isdn) as num_isdn
from 
    (select partition, g_volume, sub_type, infras, num_register_day, isdn
    from f121_tot_charge_accum_final
    where partition in ('2020101','2020102','2020103','2020104')) a
group by partition, 
    case when a.g_volume = 0 then '0MB'
         when a.g_volume <=10 and a.g_volume > 0 then '0-10MB'
         when a.g_volume <=50 and a.g_volume >10 then '10 - 50MB'
    end as data;

但是SQL表示这样的错误

**Query execution failed
Reason:
SQL Error: org.apache.spark.sql.catalyst.parser.ParseException: 
mismatched input 'as' expecting {<EOF>, ',', '.', '[', 'GROUPING', 'ORDER', 'HAVING', 'LIMIT', 'OR', 'AND', 'IN', NOT, 'BETWEEN', 'LIKE', RLIKE, 'IS', 'WINDOW', 'WITH', 'UNION', 'EXCEPT', 'INTERSECT', EQ, '<=>', '<>', '!=', '<', LTE, '>', GTE, '+', '-', '*', '/', '%', 'DIV', '&', '|', '^', 'SORT', 'CLUSTER', 'DISTRIBUTE'}(line 15, pos 5)**

有人可以帮我吗。我不明白为什么。

1 个答案:

答案 0 :(得分:1)

您的分组依据具有列别名定义:

case when a.g_volume = 0 then '0MB'
     when a.g_volume <=10 and a.g_volume > 0 then '0-10MB'
     when a.g_volume <=50 and a.g_volume >10 then '10 - 50MB'
end as data;
----^

您需要删除它。列别名只能在select中定义:

group by partition, 
    case when a.g_volume = 0 then '0MB'
         when a.g_volume <=10 and a.g_volume > 0 then '0-10MB'
         when a.g_volume <=50 and a.g_volume >10 then '10 - 50MB'
    end ;

也就是说,我认为Hive可以允许在group by中的职位:

group by 1, 2;