sql - Concat,然后在Hive中分组

标签 sql hadoop hive hiveql

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

相关文章:

c# - LINQ 列中的方法调用

sql - 计算 SQL 中 CRON 表达式的下一次运行时间

hadoop - 没有启动数据节点

apache - 无法实例化 org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient

java - 使用 Hive JDBC 执行程序时出错

hadoop - hive 服务器 : ClassNotFound (HiveServer)

mysql - 数据库设计反馈

sql - 使用 PostgreSQL 将 future 的月份加 1

java - hadoop容器标准输出始终为空

hadoop - 类型为 boolean 的分区在 Hive 中总是为真