SQL / Impala将嵌套查询分解为更易读的格式

时间:2016-08-10 21:55:32

标签: python sql impala

我有以下工作python代码来进行impala连接/查询:

import pandas as pd


query = 'select my_c_instance_id, count(my_c_instance_id) as my_ins_id_count from ' + \
         '(select * from my_table where my_c_id like "%small%") as small_table' + \
         ' group by(my_c_instance_id)'
cursor = impala_con.cursor()
cursor.execute('USE my_db')
cursor.execute(query)
df_result = as_pandas(cursor)
df_result

代码工作正常,但我想知道是否可以将其分成两个更易读的部分,例如:

small_table = 'select * from my_table where my_c_id like "%small%"'

query = 'select my_c_instance_id, count(my_c_instance_id) as my_ins_id_count from small_table group by(my_c_instance_id)'

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

如果可能的话,我如何让上述想法真正起作用?感谢。

1 个答案:

答案 0 :(得分:1)

除非我误解了某些内容,否则根本不需要子查询,只需将where条件移至主查询:

select my_c_instance_id, count(my_c_instance_id) as my_ins_id_count 
from my_table 
where my_c_id like '%small%'
group by my_c_instance_id