我在下表中有3列,如下所示:
|---------------------|------------------|-------------|
| dept | class | item |
|---------------------|------------------|-------------|
| 234 | 34 | 6783 |
|---------------------|------------------|-------------|
| 784 | 78 | 2346 |
|---------------------|------------------|-------------|
当我串联3列并将一列创建为“item_no”(值234-34-6783)时,
在按功能分组使用新列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 ;
关于sql - Concat,然后在Hive中分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61321729/