'When using read_sql_query in pandas, how to write the SQL across multiple lines?

my question is pretty much what it sounds like: Is it possible to write my SQL across multiple lines for ease of reading when using the read_sql_query method please? For example, to make this:

v_df = pd.read_sql_query(
    sql = "SELECT EVENT_INVITE, COUNT(ACCOUNT_NUMBER) AS CUSTOMERS FROM CS.SM_MAR22_FINAL GROUP BY EVENT_INVITE ORDER BY EVENT_INVITE;",
    con = v_conn)

Look something more like this:

v_df = pd.read_sql_query(
    sql = " SELECT EVENT_INVITE, COUNT(ACCOUNT_NUMBER) AS CUSTOMERS 
            FROM CS.SM_MAR22_FINAL 
            GROUP BY EVENT_INVITE 
            ORDER BY EVENT_INVITE
            ;",
    con = v_conn) 

Any help would be much appreciated.



Solution 1:[1]

python multiline string is your best bet here. So in your case:

v_df = pd.read_sql_query(
    sql = """
          SELECT EVENT_INVITE, COUNT(ACCOUNT_NUMBER) AS CUSTOMERS 
          FROM CS.SM_MAR22_FINAL 
          GROUP BY EVENT_INVITE 
          ORDER BY EVENT_INVITE
          ;""",
    con = v_conn) 

The answer here adds some details about formatting and comments

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cyprus247