Impala / SQL:选择一个子表并加入

时间:2016-08-12 22:46:19

标签: python sql impala

我正在尝试使用以下代码查找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

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

1 个答案:

答案 0 :(得分:1)

这是因为您的查询语法错误。您不能使用条件语句的别名,如下所示。别名仅用于表名和列名。

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";