Impala/SQL:select一分table并加入

Impala/SQL: select a sub-table and join

我正在尝试使用以下代码在 table_1 中查找上个月的数据,然后将其与 table_2 左连接:

import pandas as pd

query = 'select * from table_1 where table_1.ts > "2016-07-12 00:00:00" as recent_table left join table_2 on table_1.t2__fk=table_2.id' 

cursor = impala_con.cursor()
cursor.execute('USE my_db')
cursor.execute(query)
df_result = as_pandas(cursor)
df_result

但出现以下错误:

HiveServer2Error: AnalysisException: Syntax error in line 1:
...s > "2016-07-10 00:00:00" as recent_table left join table_2...
                             ^
Encountered: AS
Expected: AND, BETWEEN, DIV, GROUP, HAVING, ILIKE, IN, IREGEXP, IS, LIKE, LIMIT, NOT, OFFSET, OR, ORDER, REGEXP, RLIKE, UNION

CAUSED BY: Exception: Syntax error

有人知道我在这里错过了什么吗?实现这一目标的正确方法是什么。谢谢!

那是因为你的查询语法有误。您不能为条件语句使用别名,如下所示。别名仅用于 table 名称和列名称。

where table_1.ts > "2016-07-12 00:00:00" as recent_table

正确的查询应该是

select t1.* 
from table_1 t1
left join table_2 t2 on t1.t2__fk = t2.id
where t1.ts > "2016-07-12 00:00:00";