2016-04-18 71 views
0

我想在下面的腳本中產生按重量計算的頂級人物。我有一個工作版本,遠低於其250返回馬特假日爲重,而現在這是所有我想與最大權重的球員,他不僅沒有任何人試圖找到在HIVE中的選擇語句的最大值

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

你有一個聚合函數,爲了得到你想要的結果,你需要通過

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; 

表達式,表達式2,使用組.. 。expression_n 未被封裝在聚合函數中且必須包含在SQL語句末尾的GROUP BY子句中的表達式 http://www.techonthenet.com/sql/group_by.php

這可能是因爲同樣的規則HiveQL也存在

+0

你不能用SELECT DISTINCT和集團在同一查詢? – dedpo

+0

否,如果您在查詢中使用聚合函數(avg,max,min ..)和group by,則需要在group by中添加其他列。例如,從table1組中選擇a,b,max(c)by a,b)你必須添加a和b來分組。你可以使用不同的,它只是聲明只需要唯一的結果。 –

+0

這是我修正你的腳本(f.name)後應該是n.fname。並且這是錯誤Failed:ParseException缺失)''''在'lname'附近行8:25在''''附近'lname'缺少EOF – dedpo