2016-08-12 5 views
0
私はのtable_1で先月データを検索するには、次のコードを使用しようとしています

が、その後、table_2でそれに参加左参加:インパラ/ SQL:サブテーブルを選択し、

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

これは、クエリの構文が正しくない原因です。下記のように、条件文のエイリアスは使用できません。エイリアスはテーブル名とカラム名にのみ使用されます。

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

おかげだろう。次に、別のテーブルと結合する前にサブテーブルを見つけるという目標をどのように達成するのですか? – Edamame

+0

@Edamame、編集した回答を確認しましたか?正しいクエリも含まれています。 – Rahul

+0

ここで正しいクエリのt1は何ですか?それともtable_1であるべきですか?ありがとう! – Edamame