0

我试图在下面的脚本中按重量产生一个最高人物。我有一个工作版本的方式,它以 250 的重量返回 Matt Holiday,现在这就是我想要的最大体重的球员,只有他不是其他人

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
4

1 回答 1

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 中也存在相同的规则

于 2016-04-18T18:04:16.587 回答