[英]Pass list of dates into SQL query
I want to run a SQL query for pairs of dates from 2020 to now.我想对从 2020 年到现在的日期对运行 SQL 查询。 I can make the list in this loop:
我可以在这个循环中制作列表:
rep_dates = list(pd.date_range(start='2020-07-31',
end='2022-09-30',
freq='1M').strftime('%Y-%m-%d'))
df_list = [0 for i in range(len(rep_dates))]
for i in range(len(rep_dates)):#, obs_date in enumerate(rep_dates):
df_list[i]=[rep_dates[i-1],rep_dates[i]]
df_list.pop(0)
Now I want to run this SQL query:现在我想运行这个 SQL 查询:
staging=run_sql('''select
cast('{prev_rep_date}' as date) as reporting_date
from (select * from database where l_reportdate = '{prev_rep_date}') as m_1
join (select * from database where l_reportdate = '{rep_date}') as m on m.brw_pk = m_1.brw_pk
''')
With my pairs labelled as prev_rep_date and rep_date - how can I pass them to the query?我的对标记为 prev_rep_date 和 rep_date - 我如何将它们传递给查询?
Thanks for any suggestions: :)感谢您的任何建议::)
If I understand correcty, f-strings should solve your problem.如果我理解正确,f-strings 应该可以解决您的问题。 This will run your query for all the pairs, probably you should store the results from the loop in a dataframes or lists, thats up to you.
这将运行您对所有对的查询,可能您应该将循环的结果存储在数据帧或列表中,这取决于您。
for x in df_list:
prev_rep_date = x[0]
rep_date = x[1]
sql_stmt = f'''
select
cast('{prev_rep_date}' as date) as reporting_date
from (select * from database where l_reportdate = '{prev_rep_date}') as m_1
join (select * from database where l_reportdate = '{rep_date}') as m on m.brw_pk = m_1.brw_pk
'''
staging = run_sql(sql_stmt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.