试图在HIVE中找到最大的选择陈述

时间:2016-04-18 17:28:31

标签: sql hadoop apache-hive

我试图在下面的脚本中按重量产生最重要的人物。我有一个工作版本的方式下面返回马特假日250重量,现在这就是我想要的最大重量的球员,他只有他没有其他人

SELECT DISTINCT n.fname, n.lname, MAX(n.weight) FROM master n 
JOIN (SELECT b.id as id, b.year as year, b.triples as triples FROM batting b 
WHERE year == 2005 AND triples > 5) x
ON x.id = n.id
ORDER BY n.weight DESC;

现在出现了像这样的错误

Failed: Semantic Exception [Error 10128]: Line 4:34 Not yet supported place for UDAF 'MAX'

但是这个脚本会返回我预期的内容,输出

SELECT DISTINCT n.fname, n.lname, n.weight FROM master n 
JOIN (SELECT b.id as id, b.year as year, b.triples as triples FROM batting b 
WHERE year == 2005 AND triples > 5) x
ON x.id = n.id
ORDER BY n.weight DESC;

输出

Matt Holiday 250
Bill Dickey 205
Bob Feller 200
Tom Glavine 190

1 个答案:

答案 0 :(得分:1)

你有一个聚合函数,为了获得你想要的结果,你需要使用group by

SELECT n.fname, n.lname, MAX(n.weight) FROM master n 
JOIN (SELECT b.id as id, b.year as year, b.triples as triples FROM batting b 
WHERE year == 2005 AND triples > 5) x
ON x.id = n.id
GROUP BY n.fname,n.lname
ORDER BY n.weight DESC
LIMIT 1;

参数或参数

SELECT expression1, expression2, ... expression_n, 
       aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;

expression1,expression2,... expression_n 表达式未封装在聚合函数中,必须包含在SQL语句末尾的GROUP BY子句中 http://www.techonthenet.com/sql/group_by.php

这可能是因为HiveQL中存在相同的规则