SQL join子句中的table1。{0} == table2。{0}是什么意思?

时间:2019-08-21 14:48:03

标签: python sql pandasql

我正在尝试理解以下代码片段:

    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表进行操作。你可以假设 以上数据框为SQL表。

表的定义在注释中。

t1.{0} == t2.{0}是什么意思? {0}在上下文中代表什么?

1 个答案:

答案 0 :(得分:1)

args = commandArgs() scriptName = args[substr(args,1,7) == '--file='] if (length(scriptName) == 0) { scriptName <- rstudioapi::getSourceEditorContext()$path } else { scriptName <- substr(scriptName, 8, nchar(scriptName)) } pathName = substr( scriptName, 1, nchar(scriptName) - nchar(strsplit(scriptName, '.*[/|\\]')[[1]][2]) ) 将格式化字符串并将列注入sqlcode.format(column)

{0}表示格式将使用第一个参数。

0将显示“这是一个字符串”