SQL selecting multiple MAX and MIN - Python - python

I have a program which stores scores for students within a database. I want to select the max score for each student. At the moment, only one student with the highest score out of all students is returned, and not the highest score for each student. Can anyone help me out with this ? (FYI there is one "score" column which has a list of scores stored in it)
def ClassSort():
myFile = sqlite3.connect("scores.db")
c = myFile.cursor()
clear()
classNo = input("Enter the class number you would like to sort... ")
clear()
type = input("How would you like to sort ? \n 1 - Highest score, with students in alphabetical order \n 2 - Highest score, highest to lowest \n 3 - Average score, highest to lowest \n... ")
if type == "1":
c.execute('SELECT MIN(score), name FROM scores WHERE classNo = (?)', (classNo,))
if type == "2":
c.execute('SELECT MAX(score), name FROM scores WHERE classNo = (?)', (classNo,))
if type == "3":
c.execute('SELECT AVG(score), name FROM scores WHERE classNo = (?)', (classNo,))
row = c.fetchall()
row = ', '.join(map(str, row))
print(row)
myFile.commit()
myFile.close()

You need to add a GROUP BY statement. Aggregation functions in sql are usually most useful with a GROUP BY.
SELECT MAX(score), name FROM scores WHERE classNo = (?) GROUP BY name

Related

Count specific information in .db file

I wish to count the number of males and females in a specific city that the user chose. Here's a gist of the .db file:
example_table
CODE AGE SEX CITY
---- --- --- ----
E101 25 M New York
E102 42 F New York
E103 31 M Chicago
E104 67 F Chicago
This is what I've coded so far based on the references I've read:
city=input("Input city: ")
import sqlite3
db = sqlite3.connect('covid.db')
cursor = db.cursor()
sql = 'SELECT Sex, COUNT(Code) FROM example_table GROUP BY Sex'
data = cursor.execute(sql).fetchall()
for sex, cases in data:
print(sex, ':', cases)
cursor.close()
So far, that prints the overall number of males and females in the data. I'd like to ask how could I print the exact number of males and females in a city? Say I input "New York", the result would be:
M : 1
F : 1
Add a WHERE clause restricting to a certain city, e.g.
sql = 'SELECT Sex, COUNT(Code) FROM example_table WHERE CITY = ? GROUP BY Sex'
params = ('New York',)
data = cursor.execute(sql, params).fetchall()
for sex, cases in data:
print(sex, ':', cases)

Sum the count within a SQLITE query

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)

Get a specific value from a table with using a few conditions in python

I have a table that each different values in the table is the result of a combination of different conditions.For example in the below image the condition is as follow: if coverType=fallow , Treatment=Crop Residue Cover, Condition/ImperviousArea=Good, SoilType=C then the value is equal to 83.I want to have a tool which asks the user to choose a value from each column (e.g. choose CoverType; SoilType, ...) and then return the related number as output. Do you have any thoughts how should I do this?
So far I have just the first lines of the code as below:
import arcpy
path= r'H\Python\FinalProject\RunOff.gdb'
arcpy.env.workspace= path
arcpy.env.overwriteOutput = True
table=r'H\Python\FinalProject\RunOff.gdb\CN.csv'
cursor= arcpy.SearchCursor(table)
Unless you are restricted to ArcGIS 10.0 or earlier, I suggest using the da cursors. The syntax is a little easier.
However, you're definitely on the right track. The cursor lets you loop through the table and find a row in which the three categories match the user's input.
cover_type = #userinput
treatment = #userinput
condition = #userinput
field_list = ["CoverType", "Treatment", "Condition", "B"]
with arcpy.da.SearchCursor(table, field_list) as cursor:
for row in cursor:
if row[0] == cover_type and row[1] == treatment and row[2] == condition:
print row[3]
else:
print "no matching combination"

Get corresponding row properties for min value in column from database table

I have a MySQL table with the following data:
ID, PERSON_NAME, SCORE, DATE
I'm trying to write a query where I can get the minimum SCORE value and the corresponding PERSON_NAME value. I've tried the following query:
min_query = "SELECT PERSON_NAME, MIN(SCORE) FROM HTMLSCORES"
cursor.execute(min_query)
result = cursor.fetchone()
name = result[0]
min_score = result[1]
However, the name value is always the same and seems to be the first entry in the table. I would like to get the corresponding PERSON_NAME value for the MIN(SCORE). How do I go about writing that query?
Try this:
SELECT PERSON_NAME, SCORE
FROM HTMLSCORES
WHERE SCORE =
(SELECT MIN(SCORE)
FROM HTMLSCORES)
Look at the sub-query first. We can find the MIN score from your table using the MIN function. Then, in the main query, we select the row or rows where score in that row matches the sub-query score.
Your first attempt doesn't work because the query is looking at the person name and the score independently. Writing the query this way will link them together.
min_query = "SELECT PERSON_NAME, MIN(SCORE) AS 'Min_Score' FROM HTMLSCORES GROUP BY PERSON_NAME ORDER BY PERSON_NAME"
This will return a list of all PERSON_NAME and their min score.
If you are after a particular user and know what the PERSON_NAME should be use
pyPerson = 'Name Here'
min_query = "SELECT PERSON_NAME, MIN(SCORE) AS 'Min_Score' FROM HTMLSCORES WHERE PERSON_NAME = '" + str(pyPerson) + "' GROUP BY PERSON_NAME"
Forgive my crude python but it should work SQL wise.

How to print a table based on an average of multiple scores (SQLite, Python)

I've been working on a program that asks a student to complete 10 mathematical questions, it then logs the users First name, Second name and Score based on their inputs at the beginning into a database using SQLite 3.
I would like the program to print a table based on the average score of all that students attempts at the quiz. For example, these two users average would be that of 8.5:
I currently use tabulate 0.7.5 to handle the table method, as done here with the sorting by score:
if sort == "score":
print("Accessing Database.")
time.sleep(2)
print("Accessing Database..")
time.sleep(2)
print("***Reading Complete***")
con = lite.connect('students.db')
with con:
cur = con.cursor()
table = cur.execute("SELECT FirstName, SecondName, Score FROM Class1 ORDER BY Score DESC")
print(tabulate(table, headers=["FirstName","SecondName", "Score"]))
I tried using a SQLite parameter to do it, but it just pasted a single average. I'd truly like a separate average that covers every attempt the student has made.
Any help would be much obliged!
Something like:
select
firstname, lastname, avg(score) as score
from
class1
group by
firstname, lastname
order by
avg(score) desc
Let the database engine do the work for you!

Categories

Resources