I'm trying to create a Postgres script that needs to declare/update variables, return in a Pandas DataFrame.
This code executes:
conn = db_connection_object('sqlalchemy')
sql_query= ''' WITH my_cte as (
select obs1
from my_data_table)
SELECT * FROM my_cte
'''
df = pd.read_sql_query(sql=sql_query,con=conn)
print(df)
This code crashes (notice DECLARE x integer;
) :
conn = db_connection_object('sqlalchemy')
sql_query= '''
DECLARE x integer;
WITH my_cte as (
select obs1
from my_data_table)
SELECT * FROM my_cte
'''
df = pd.read_sql_query(sql=sql_query,con=conn)
print(df)
A simplified example of final use case (I know variables unneeded for this result, only to illustrate):
conn = db_connection_object('sqlalchemy')
sql_query= '''
DECLARE x integer;
y float8;
WITH my_cte as (
select obs1
from my_data_table)
x = select count(obs1) FROM my_cte
y = select avg(obs1) FROM my_cte
SELECT x, y
'''
df = pd.read_sql_query(sql=sql_query,con=conn)
print(df)
I've read the PostgreSQL documentation, and several questions related to using pgAdmin4 and DBeaver as a query writing tools. I tried using pgAdmin4/ DBeaver for query creation and testing but they have different ways of handling variables and I can't tell if either syntax will work in my end use case.
Obviously I'd like to know how to accomplish the main question, and if anyone has a suggestion of a tool that uses the same syntax as my final use I'd be thankful to know :)
Thank you for helping me!