【问题标题】:Invalid table alias or column reference b无效的表别名或列引用 b
【发布时间】:2015-04-18 00:51:36
【问题描述】:

这个查询有什么问题(在 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

我认为它应该很简单,但我明白了:

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

这是 YYY 表:

hive> desc YYY;
OK
status_code     int
uid    string
zip_code        string
keyword string
region_code     bigint
dt      timestamp
channel int

和XXX表:

hive> desc XXX;
OK
group_key     string
category    string
uid    string
dt      timestamp

【问题讨论】:

  • 错误是说您的YYY 表不包含region_code 列。如果您向我们展示您的 2 个表的表结构,它将对我们更有帮助。
  • 添加了表结构

标签: sql hive


【解决方案1】:

尝试这样做:

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

您的代码的问题是b.region_codeorder by 之后不存在。 别名 存在 (region_code),因为它在 select 中。 合格的别名没有,因为bgroup by 之后不再有效。我想你可以写:

order by max(b.region_code)

但在这种情况下那将是愚蠢的。

请注意,这对所有数据库都是通用的,但 MySQL 除外。

【讨论】:

  • 谢谢,以这种方式运行它看起来没有语法错误。我试图理解为什么在 group by 之后不存在 b.region 代码,你能帮我解释一下吗?什么是别名与限定别名?
  • @丹尼尔。 . .合格的别名意味着该列前面有表名——这是一个好习惯,您通常会这样做。您可以将问题视为order by 只知道select 中定义的表达式。 b.region_code 是一个名称为 region_code 的表达式。这不是 100% 正确的,但我认为它有助于您对所看到的行为的直觉。
猜你喜欢
  • 2019-07-12
  • 2020-04-24
  • 1970-01-01
  • 2023-03-30
  • 2021-09-16
  • 1970-01-01
  • 2023-03-26
  • 2020-02-04
  • 2019-03-31
相关资源
最近更新 更多