0

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!

Programming_Learner_DK
  • 1,509
  • 4
  • 23
  • 49
  • Possibly related: https://stackoverflow.com/q/4990622/2144390 – Gord Thompson Apr 01 '23 at 14:37
  • SQL does not have "variables". Only PL/pgSQL does. But you can't just mix PL/pgSQL and SQL the way you do it. But if you are running SQL from withing a programming language, then use variables in that language and pass them to the query –  Apr 01 '23 at 15:24
  • @a_horse_with_no_name, thx for feedback. I know how to pass variables through python for col names, etc. However, I want to run calculations in the db rather than pull the data out, store variable, pass back to db and repeat a couple of times to get final answer. Using local variables inside the db (PL/pgSQL) would help my cause. – Programming_Learner_DK Apr 01 '23 at 15:43

0 Answers0