0

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.

SRJCoding
  • 335
  • 2
  • 15
  • 2
    use python multiline strings `""" SELECT ... ; """` with triple quotes. – Dima Chubarov May 06 '22 at 08:21
  • 1
    This is basic Python syntax, it has nothing to do with pandas. – Barmar May 06 '22 at 08:22
  • Does this answer your question? [Pythonic way to create a long multi-line string](https://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string) –  May 06 '22 at 09:01

1 Answers1

2

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

cyprus247
  • 36
  • 3