2016-07-17 17 views
0

子查詢我與模式的表,如下圖所示:收到一個錯誤而執行蜂房查詢涉及在FROM子句

CREATE TABLE Table1 (`membertype` varchar(1)); 

INSERT INTO Table1 (`membertype`) 
VALUES 
    ('Y'),('H'),('U'),('W'),('W'),('W'),('H'),('H'),('U'), 
    ('U'),('P'),('P'),(''),('P'),('P'),('P'),(''),('W'), 
    ('Y'),('Y'),('Y'),('H'),('D'),('D'),('D'),('D'),('H'), 
    ('W'),('W'); 

我試圖做到的,是讓每一個計數在列中輸入以及它們的百分比與列中類型的總數。

我測試在MySQL中下面的查詢,並得到所期望的結果:

select (case when m.membertype='Y' then 'Young Adult' 
    when m.membertype='H' then 'Head' 
    when m.membertype='W' then 'Spouse' 
    when m.membertype='P' then 'Aged Parent' 
    when m.membertype='U' then 'Unknown' 
    when m.membertype='D' then 'Deceased' else 'No Match' end) as type, 
    count(*) as typecount, 
    count(*)/t.total*100 as percentage from Table1 as m, 
    (select count(*) as total from Table1) as t group by membertype; 

type  typecount percentage 
No Match  2   6.8966 
Deceased  4   13.7931 
Head   5   17.2414 
Aged Parent 5   17.2414 
Unknown  3   10.3448 
Spouse  6   20.6897 
Young Adult 4   13.7931 

**

Link

**

相同的查詢在HIVE失敗,並在下面錯誤:

select (case when h.member='Y' then 'Young Adult' 
when h.member='H' then 'Head' 
when h.member='W' then 'Spouse' 
when h.member='P' then 'Aged Parent' 
when h.member='U' then 'Unknown' 
when h.member='D' then 'Deceased' else 'No Match' end) as type, 
count(*) as typecount, 
count(*)/t.total*100 as percentage from hhinfo as h, 
(select count(*) as total from hhinfo) as t group by member; 


FAILED: SemanticException [Error 10025]: Line 1:277 Expression not in GROUP BY key 'total' 

我在這裏錯過了什麼?任何幫助,將不勝感激。

+0

你有沒有嘗試total'明確添加'的'組by'條款? '...作爲t團成員,共有' –

+0

我多麼愚蠢......那就是訣竅。非常感謝你@JaimeCr :) –

回答

0

嘗試顯式添加totalgroup by clause

...as t group by member, total

+0

再次感謝Buddy :) –