Hive:SELECT AS和GROUP BY

时间:2012-09-26 10:50:31

标签: hadoop hive

我有像

这样的Hive查询
SELECT Year, Month, Day, Hours, Minutes,
           cast((cast(Seconds as int)/15) as int)*15
AS secondMod, Count(*) AS PerCount FROM LoggerTable 
 GROUP BY Year, Month, Day, Hours, Minutes, secondMod 
ORDER BY PerCount;

上述查询失败并显示错误

  

失败:语义分析出错:第1行:175无效表别名或列引用secondMod

'LoggerTable'是一个包含所有字符串类型列的Hive表。

此问题的解决方法是什么?

2 个答案:

答案 0 :(得分:12)

试试这个:

SELECT Year, Month, Day, Hours, Minutes, 
cast((cast(Seconds as int)/15) as int)*15 
AS secondMod, Count(*) AS PerCount FROM LoggerTable 
 GROUP BY Year, Month, Day, Hours, Minutes, 
   cast((cast(Seconds as int)/15) as int)*15
ORDER BY PerCount;

答案 1 :(得分:3)

在Hive 0.11.0及更高版本中,如果hive.groupby.orderby.position.alias设置为true,则可以按位置指定列。 请确认以下查询是否适合您。

SET hive.groupby.orderby.position.alias=true;
SELECT Year
       ,Month
       ,Day
       ,Hours
       ,Minutes
       ,cast((cast(Seconds as int)/15) as int)*15 AS secondMod
       ,count(*) AS PerCount 
FROM LoggerTable 
GROUP BY 1, 2, 3, 4, 5, 6
ORDER BY 7;