【发布时间】:2018-01-20 03:06:05
【问题描述】:
我有以下代码来计算表中特定行的值:
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
我需要做的是将percentile 结果添加到我通过SELECT 查询获得的每条记录的相应列score_percentile 中。
我想过这样一个UPDATE 查询:
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)")
但我不知道该查询是否正确,或者是否有更有效且不那么愚蠢的方法来做到这一点(我相信必须有)。 你能帮帮我吗?
我是 SQL 新手,因此非常感谢任何帮助或建议。 谢谢!
【问题讨论】:
标签: python sql postgresql python-2.7 psycopg2