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!