HIVE中的不同计数和分组

时间:2017-11-01 16:32:12

标签: hive hiveql

我对HIVE非常陌生,并且有明显的计数和GROUP BY的问题 我想从temperature_data表计算最高温度,该表对应于表中至少有2个条目的年份 - 我尝试使用以下查询,但它无法正常工作

select 
SUBSTRING(full_date,7,4) as year, 
MAX(temperature) as temperature 
from temperature_data 
where count(distinct(SUBSTRING(full_date,7,4))) >= 2 
GROUP BY SUBSTRING(full_date,7,4);

我收到错误 -

FAILED:SemanticException [错误10128]:第2行:0 UDAF尚未支持的地方' count'

以下是输入 -

年,拉链,温度
10-01-1990,123112,10
14-02-1991,283901,11
10-03-1990,381920,15
10-01-1991,302918,22
12-02-1990,384902,9
10-01-1991,123112,11
14-02-1990,283901,12
10-03-1991,381920,16
10-01-1990,302918,23
12-02-1991,384902,10
10-01-1993,123112,11

2 个答案:

答案 0 :(得分:2)

您应该使用 HAVING 关键字来设置您用于分组的变量的条件。

此外,您可以使用子查询。见下文。

SELECT 
  year, 
  MAX(t1.temperature) as temperature 
FROM
  (select SUBSTRING(full_date,7,4) year, temperature  from temperature_data) t1 
GROUP BY 
  year
HAVING 
  count(t1.year) > 2;

答案 1 :(得分:0)

@ R.Gold,我们可以尝试简化上述查询而不使用下面的子查询,

从你的hive-table中选择substring(full_date,7)作为年份,max(温度) group by substring(full_date,7) 有计数(substring(full_date,7))> = 2

并且,fyi - 我们不能将聚合函数与WHERE子句一起使用。

相关问题