试图在 HIVE 中找到 select 语句的最大值

trying to find the max of select statement in HIVE

我试图在下面的脚本中产生一个按权重排名第一的人。我有一个工作版本,低于 returns Matt Holiday 体重 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'

但是这个脚本 returns 如我所料,输出如下

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

你有一个聚合函数,为了得到你想要的结果,你需要使用 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;

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

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