1

Current code:

 import pyodbc

connection = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

if connection:
        print("Yes we connected\n")

cur = connection.cursor()

cur.execute(SELECT cost1 FROM tbl)

data = cur.fetchall()

for row in data:
        print(row)

I admit I have almost no idea what I am doing in python and I got slapped with a project a little while ago that is do ASA-yesterday. I need to be able to take the results from the SQL query and sum them together. Or store them in variables to where I can call back to them in an equation.

Results are: 10, 431, 543, 1268, 1207

Expected Result: 3459

I am more than happy to give any more information, and I will be spending the interim continuing to research.

3
  • you can sum the row values if they integer if not convert them to integer before the addition Commented Mar 19, 2018 at 20:03
  • They are number values, they actually have decimals. I just truncated them for the example. Can I still sum the rows if they are numbers values? And is it just print(sum(row))? Commented Mar 19, 2018 at 20:06
  • No, data is a list which contains numbers, row is just one of those numbers so you can't use sum(row). You can add row values to a variable one-by-one but you can use sum function for data Commented Mar 19, 2018 at 20:09

3 Answers 3

3

If you are only after the sum and not the individual numbers then the easiest way is to do that in SQL:

query = 'select sum(cost1) from tbl'

If you want the individual numbers also then.:

cur.execute(SELECT cost1 FROM tbl)

data = cur.fetchall()
tot = 0
for row in data:
        print(row)
        tot += row[0]
print(tot)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I was not able to sum it in SQL as it is a much bigger query than shown. Your second result works like a charm.
1

your SQL query should be modified to SELECT sum(cost1) FROM tbl

or you can get the results in the python, and sum them... otherwise you are just getting a list of values in the column cost1

or in python you can do:

import pyodbc

connection = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

if connection:
        print("Yes we connected\n")

cur = connection.cursor()

cur.execute(SELECT cost1 FROM tbl)

data = cur.fetchall()
sum = 0
for row in data:
        sum = sum + row
print(sum)

2 Comments

The SQL query is actually massive, and I can't sum it there unfortunately. I've been doing some research and cannot find a solution the works for summing the columns in python.
added a pythonic solution for the sum as well
0

You don't need to go adding row by row:

import pyodbc
connection = pyodbc.connect('DRIVER={ODBC Driver 13 for SQLServer};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

if connection:
    print("Yes we connected\n")

cur = connection.cursor()
cur.execute('SELECT sum(cost1) FROM tbl;')
x=cur.fetchone()[0]
print(x) # x will hold the value of the sum

Have a great day.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.