Concat 然后在 Hive 中分组

Concat and then group by in Hive

我在 table 中有 3 列,如下所示:

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

当我连接 3 列并将列创建为 'item_no'(值 234-34-6783)时, 当我按功能分组使用新列 item_no 时它会抛出错误 - 'Invalid table alias or column reference' 有人可以帮我解决这个问题吗?

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 是 table 中的一列,这将不起作用。并且位置符号也有效:

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