SQL 连接子句中的 table1.{0} == table2.{0} 是什么意思?
What's the meaning of table1.{0} == table2.{0} in SQL join clause?
我正在尝试理解以下代码片段:
improt pandasql
data_sql = data[['account_id', 'id', 'date', 'amount']]
# data_sql is a table has the above columns
data_sql.loc[:, 'date_hist_min'] = data_sql.date.apply(lambda x: x + pd.DateOffset(months=-6))
# add one more column, 'date_hist_min', it is from the column 'data' with the month minus 6
sqlcode = '''
SELECT t1.id,
t1.date,
t2.account_id as "account_id_hist",
t2.date as "date_hist",
t2.amount as "amount_hist"
FROM data_sql as t1 JOIN data_sql as t2
ON (cast(strftime('%s', t2.date) as integer) BETWEEN
(cast(strftime('%s', t1.date_hist_min) as integer))
AND (cast(strftime('%s', t1.date) as integer)))
AND (t1.{0} == t2.{0})
'''
# perform the SQL query on the table with sqlcode:
newdf = pandasql.sqldf(sqlcode.format(column), locals())
代码与Python pandasql。它将数据帧操作为 SQL table。你可以假设
上面的数据框为 SQL table。
table的定义在评论里。
t1.{0} == t2.{0}
是什么意思? {0}
在上下文中代表什么?
sqlcode.format(column)
正在格式化字符串并将列注入 {0}
0
表示格式将使用第一个参数。
print("This {1} a {0}".format("string", "is"))
会打印 "This is a string"
我正在尝试理解以下代码片段:
improt pandasql
data_sql = data[['account_id', 'id', 'date', 'amount']]
# data_sql is a table has the above columns
data_sql.loc[:, 'date_hist_min'] = data_sql.date.apply(lambda x: x + pd.DateOffset(months=-6))
# add one more column, 'date_hist_min', it is from the column 'data' with the month minus 6
sqlcode = '''
SELECT t1.id,
t1.date,
t2.account_id as "account_id_hist",
t2.date as "date_hist",
t2.amount as "amount_hist"
FROM data_sql as t1 JOIN data_sql as t2
ON (cast(strftime('%s', t2.date) as integer) BETWEEN
(cast(strftime('%s', t1.date_hist_min) as integer))
AND (cast(strftime('%s', t1.date) as integer)))
AND (t1.{0} == t2.{0})
'''
# perform the SQL query on the table with sqlcode:
newdf = pandasql.sqldf(sqlcode.format(column), locals())
代码与Python pandasql。它将数据帧操作为 SQL table。你可以假设 上面的数据框为 SQL table。
table的定义在评论里。
t1.{0} == t2.{0}
是什么意思? {0}
在上下文中代表什么?
sqlcode.format(column)
正在格式化字符串并将列注入 {0}
0
表示格式将使用第一个参数。
print("This {1} a {0}".format("string", "is"))
会打印 "This is a string"