gpt4 book ai didi

sql - Concat,然后在Hive中分组

转载 作者:行者123 更新时间:2023-12-02 18:49:10 24 4
gpt4 key购买 nike

我在下表中有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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com