簡體   English   中英

在 Hive 中連接然后分組

[英]Concat and then group by in Hive

我在一個表中有 3 列,如下所示:

|---------------------|------------------|-------------|
|      dept           |     class        |    item     |
|---------------------|------------------|-------------|
|          234        |         34       |      6783   |
|---------------------|------------------|-------------|
|          784        |         78       |      2346   |
|---------------------|------------------|-------------|

雖然我正在連接 3 列並將列創建為“item_no”(值 234-34-6783),但當我使用 function 組中的新列 item_no 時會引發錯誤 - '無效的表別名或列引用'有人可以幫我解決這個問題嗎?

select dept, class, item, concat(dept, '-', class, '-', item) as item_no, sum(sales)
from sales_table
group by dept, class, item, item_no;

列數據類型是 smallint

這里有兩種方法:

select concat(dept, '-', class, '-', item) as item_no, count(*)
from t
group by concat(dept, '-', class, '-', item) ;

或者:

select concat(dept, '-', class, '-', item) as item_no, count(*)
from t
group by dept, class, item ;

也就是說,我認為 Hive 支持group by中的別名,所以這也應該有效:

select concat(dept, '-', class, '-', item) as item_no, count(*)
from t
group by item_no ;

但是,如果item_no是表中的一列,這將不起作用。 位置符號也可以:

select concat(dept, '-', class, '-', item) as item_no, count(*)
from t
group by 1 ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM