2017-05-12 50 views
0

我想寫一個查詢來在每個@字符後面分割一列。然後我希望能夠在每個細分市場中對這些進行計數。如何在配置單元中合併拆分和計數

我已成功地編寫以下查詢在蜂巢:

SELECT 
distinct split (msg_txt,'\\@')[0] AS first_msg, count(*) 
FROM table1 
; 

但這不會讓我通過,以獲得計數添加組。我試圖用一個子查詢這樣做:

SELECT first_msg, count(*) 
FROM (
SELECT 
distinct split (msg_txt,'\\@')[0] AS first_msg 
FROM table1 
) 
GROUP BY first_msg 
; 

,但是這給了我以下錯誤:

Error while compiling statement: FAILED: ParseException line 7:6 missing EOF at 'BY' near 'GROUP' 

所以不知道我怎麼能寫這個查詢。

如果有人可以請建議真的很感激。

在此先感謝。

回答

0

我想你只需要一個表的別名:

SELECT first_msg, count(*) 
FROM (SELECT distinct split(msg_txt,'\\@')[0] AS first_msg 
     FROM table1 
    ) t 
GROUP BY first_msg; 

Hive需要一個表的別名:

The subquery has to be given a name because every table in a FROM clause must have a name.

在你的版本,它把GROUP作爲子查詢的名稱。 BY然後沒有意義。

書面,這有點無意義的,因爲你可以這樣做:

SELECT distinct split(msg_txt,'\\@')[0] AS first_msg, 1 as cnt 
FROM table1; 

子查詢的distinct將確保所有值都是唯一的。我假設你的實際問題稍微複雜一點。

+0

我不知道爲什麼會得出一個downvote,答案是正確的。 –

0

根據您的要求,我不確定您爲什麼得到第一個元素。忽略拆分的第一要素查詢(考慮到你要應用的組之後的所有元素「@)應該是這樣

select value, count(*) from (
select 
pos,value 
from table1 lateral view posexplode(split (msg_txt,'\\@')) explodedcol as pos,value limit 10 
) t where pos != 0 group by value 
; 

如果要包括所有要素分裂‘@’ ,只是刪除了「後!= 0」的條件從where子句。

問候,

相關問題