[英]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.