2015-11-25 120 views
0

我正在寫一個查詢,需要GROUP BY一行的類型,然後將值除以總數,以知道IMPALA中的總數的百分比。 例:使用Impala中的SELECT語句進行算術運算

Name       performance 
something type1 something   15 
something type1 something   18 
something type2 something   23 
something something something  345 
something type2 something   23 

SELECT 
CASE WHEN name like '%type1%' then 'type 1' 
    WHEN name like '%type2%' then 'type2' 
    ELSE 'other' END as type 
,sum(performance)/(SELECT sum(performance) FROM table) 
FROM table 
GROUP BY type 

這讓我AnalysisException的錯誤:子查詢不在選擇列表的支持。 任何人都可以建議我將如何解決這個問題?

+1

我不明白,如果你真的來自同一個表中提取:'總和(性能)/(SELECT SUM(性能)FROM表) FROM table' – genespos

回答

0

我認爲只需要 「()」

SELECT 
(CASE WHEN name like '%type1%' then 'type 1' 
    WHEN name like '%type2%' then 'type2' 
    Else 'other' END) as type 
,sum(performance)/(SELECT sum(performance) FROM table) 
FROM Table 
GROUP BY type