无效的表别名或列引用b

Dan*_*iel 10 sql hive

这个查询有什么问题(在hive中运行):

SELECT count(*) TotalCount, b.region_code
from XXX a
INNER JOIN YYY b
ON a.uid=b.uid
where a.dt = '2015-04-15'
group by b.region_code order by b.region_code
Run Code Online (Sandbox Code Playgroud)

我认为它应该非常简单,但我得到了这个:

FAILED: SemanticException [Error 10004]: Line 6:32 Invalid table alias or column reference 'b': (possible column names are: _col0, _col1)
Run Code Online (Sandbox Code Playgroud)

这是YYY表:

hive> desc YYY;
OK
status_code     int
uid    string
zip_code        string
keyword string
region_code     bigint
dt      timestamp
channel int
Run Code Online (Sandbox Code Playgroud)

和XXX表:

hive> desc XXX;
OK
group_key     string
category    string
uid    string
dt      timestamp
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 10

试着这样做:

SELECT count(*) as TotalCount, b.region_code
from XXX a INNER JOIN
     YYY b
     ON a.ui = b.uid
where a.dt = '2015-04-15'
group by b.region_code
order by region_code
Run Code Online (Sandbox Code Playgroud)

你的代码的问题是b.region_code在之后不存在order by.该别名存在的(region_code),因为这是在select.在合格的别名没有,因为b是后不再有效group by.我想你可以写:

order by max(b.region_code)
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,这将是愚蠢的.

请注意,除了MySQL之外,这对所有数据库都是通用的.