Pandas read_sql query with multiple selects

前端 未结 1 1481
渐次进展
渐次进展 2020-12-16 02:45

Can read_sql query handle a sql script with multiple select statements?

I have a MSSQL query that is performing different tasks, but I don\'t want to have to write a

相关标签:
1条回答
  • 2020-12-16 03:34

    You could do the following:

    queries = """
    SELECT ColumnX_1, ColumnX_2, ColumnX_3
    
    FROM Table_X
    INNER JOIN (Etc etc...)
    ---
    SELECT ColumnY_1, ColumnY_2, ColumnY_3
    
    FROM Table_Y
    INNER JOIN (Etc etc...)
    """.split("---")
    

    Now you can query each table and concat the result:

    df = pd.concat([pd.read_sql_query(q, connection) for q in queries])
    

    Another option is to use UNION on the two results i.e. do the concat in SQL.

    0 讨论(0)
提交回复
热议问题