Sum the count within a SQLITE query - python

Within my sqlite query, I'm attempting to not only get the count but also sum the count. This is what I have so far:
sql = c.execute("SELECT DISTINCT Org, COUNT(*) AS NUM FROM 2014Data WHERE Action='UPDATED' AND NewValue='' GROUP BY Org ORDER BY NUM DESC")
sql_list = list(sql)
list_count = len(sql_list)
print("2014 Data:")
for i in sql_list:
print(i)
sql_sum = c.execute("SELECT SUM(NUM) FROM (SELECT DISTINCT Org, COUNT(*) AS NUM FROM 2014Data WHERE Action='UPDATED' AND NewValue='')")
sum_list = list(sql_sum)
print("Sum =", sum_list)
print()
I get the correct output for the first sql query, but I don't get the correct output for the second.
Any help would be much appreciated!
Thanks in advance!

If you're looking to get the sum of the table from your first sql statement, then it looks like you just missed adding the group by (GROUP BY Org) you had in the first query. without having the database and the expected results, below is what I expect will work for you.
sql = c.execute("SELECT DISTINCT Org, COUNT(*) AS NUM FROM 2014Data WHERE Action='UPDATED' AND NewValue='' GROUP BY Org ORDER BY NUM DESC")
sql_list = list(sql)
list_count = len(sql_list)
print("2014 Data:")
for i in sql_list:
print(i)
sql_sum = c.execute("SELECT SUM(NUM) FROM (SELECT DISTINCT Org, COUNT(*) AS NUM FROM 2014Data WHERE Action='UPDATED' AND NewValue='' GROUP BY Org )")
sum_list = list(sql_sum)
print("Sum =", sum_list)

Related

Unclear behavior in for-loop

I have this code in python:
query = "SELECT product_id FROM product_orders WHERE table_number = "+e
cursor.execute(query)
records = cursor.fetchall()
for record in records:
query2 = "SELECT * FROM productss WHERE id = "+str(record[0])
cursor.execute(query2)
record2 = cursor.fetchall()
sum=0
for record1 in record2:
sum = sum + record1[2]
tree.insert("", tk.END, values=record1)
tree2.insert("", tk.END, values=sum)
The problem is the sum variable does not make summing, but stores only the last value of record1. Any solution for this?
It looks like you have sum = 0 inside of a for loop. Maybe if you take it out of the loop and make it a list of sums that will fix your issue. Also, as another user has said, sum is a built-in name, you can name your variable s instead.

SQL query only selecting first result

I want to retrieve all songIDs that are associated with the userID that I input but it only prints the first result.
database:
My code:
enter = raw_input('Enter UserID: ')
cursor = MusicData.cursor()
sql = "SELECT * FROM (SELECT songID FROM train WHERE userID=? )"
result = cursor.execute(sql,(enter,))
print result.fetchall()[0][0],
Enter UserID: 3a613180775197cd08c154abe4e3f67af238a632
SODOZXB12A8C13CD55
You're only showing the first rows first column due to
[0][0] after in this line print result.fetchall()[0][0]
The problem is with your print statement. Right now by giving print result.fetchall()[0][0] you are asking python to print one element. Use
for item in result.fetchall():
print item
Add "LIMIT NN" to your SQL request. That will limit an output to NN number of rows:
sql = "SELECT * FROM Table LIMIT 5"
Will give you only 5 rows.
on MS SQL DB it should be the "TOP" keyword:
sql = "SELECT TOP 5 * FROM Table"
I fixed it by adding a for loop of fetchall()
rows = result.fetchall()
for row in rows:
print row

Update a table's column by using a group by

I would like to calculate the average in specific column of my table grouping by another column, but it doesn't work, I have a problem whith the group by.
My code:
import sqlite3
conn = sqlite3.connect("ma_base.db")
cur = conn.cursor()
cur.execute("UPDATE test_centrale set avg_price = avg(prix) group by test_centrale.version ")
conn.commit()
print('done')
cur.close()
If I got your question right. Use subquery to find the second average and update using following query
cur.execute("UPDATE test_centrale set avg_price = (select avg(prix) from test_centrale group by test_centrale.version )")

How can I update rows obtained by a SELECT query in PostgreSQL with Python? Python 2.7 psycopg2

I have the following code to calculate a value in specific rows of my table:
cursor.execute("SELECT * FROM restaurants WHERE license_type_code='20' ORDER BY general_score DESC;")
group_size = cursor.rowcount
for record in cursor:
index = cursor.rownumber
percentile = 100*(index - 0.5)/group_size
print percentile
What I need to do is to add the percentile result to the respective column score_percentile of each record I got with the SELECT query.
I thought about an UPDATE query like this:
cursor.execute("UPDATE restaurants SET score_percentile="+str(percentile)+" WHERE license_type_code IN (SELECT * FROM restaurants WHERE license_type_code='20' ORDER BY general_score DESC)")
But I don't know if that query is correct or if there's a more efficient and less silly way to do that (I'm sure there has to be).
Could you help me, please?
I'm new with SQL so any help or advice is highly appreciated.
Thanks!
You don't need the loop at all. Just one update query
cursor.execute("UPDATE restaurants SET score_percentile = 100*(rownumber - 0.5)/group_size FROM (SELECT COUNT (*) as group_size FROM restaurants WHERE license_type_code='20') as t WHERE restaurants.license_type_code='20'")
As Thomas said, I just needed an update query with the following syntax:
cursor.execute("UPDATE restaurants f SET score_percentile = ROUND(100*(f2.rownumber - 0.5)/"+str(group_size)+",3) FROM (SELECT f2.*,row_number() OVER (ORDER BY general_score DESC) as rownumber FROM restaurants f2 WHERE license_type_code='20') f2 WHERE f.license_type_code='20' AND f2.license_number=f.license_number;")
And I got the group_size by:
cursor.execute("SELECT COUNT(*) FROM restaurants WHERE license_type_code='20'")
group_size = cursor.fetchone()
group_size = group_size[0]
That worked perfect for my case

how to find the sum of multiple numbers from a column in a sql database in python?

I have a database that has a bookings table in. One of the columns in the bookings table is 'incomes', and another one is 'date_of_booking,' which stores dates in 'DD/MM/YYYY' format. I am trying to write a feature that lets a user input a month, and from that will calculate all the incomes from that month. So far I have this:
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sqlCmd = 'SELECT date FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
for row in conn.execute(sqlCmd):
print (row)
With this code, I am able to output the date of bookings for a particular month. However I want to output the total incomes for a particular month. What do I need to add so that this works out the total incomes for a particular month and outputs it? Any help would be gladly appreciated, thanks.
Replace one statement.
SELECT sum(income) FROM bookings where SUBSTR(date,4,2)='04'
As in:
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('CREATE TABLE bookings (date text, income real)')
c.execute('''INSERT INTO bookings VALUES ('01/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('15/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('22/04/2017', 19.22)''')
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sql = '''SELECT sum(income) FROM bookings where SUBSTR(date,4,2)="%.2i"''' % int(lookForMonth)
for row in c.execute(sql):
print (row)
Resulting output:
What month, please? (number from 1 through 12):4
(57.66,)
First of all, you want to select both in your sql statement.
sqlCmd = 'SELECT date_of_booking,incomes FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
income_sum = 0
for (row_date, row_income) in conn.execute(sqlCmd):
income_sum += row_income
print row_date
print income_sum
Then you can specify both date and income of the row in your loop like above.

Categories

Resources