from settings import get_connection_racetimer def main(): with get_connection_racetimer() as conn: with conn.cursor(buffered=True) as cur: sql_statement = """ select column_name from information_schema.columns where table_schema = 'unloze_racetimer_css' and TABLE_NAME = 'zetimer_table_new' order by table_name,ordinal_position """ cur.execute(sql_statement) res = cur.fetchall() #removing steamid and name res.pop(0) res.pop(0) sql_statement = "TRUNCATE TABLE unloze_racetimer_css.top5" cur.execute(sql_statement) for index, result in enumerate(res): result = result[0] sql_statement = f"""alter table unloze_racetimer_css.top5 add column if not exists {result} DOUBLE """ cur.execute(sql_statement) if index == 0: sql_statement = f""" insert into unloze_racetimer_css.top5 ({result}) SELECT t2.{result} FROM unloze_racetimer_css.zetimer_table_new t2 WHERE t2.{result} > 0 ORDER BY t2.{result} ASC LIMIT 5 """ else: sql_statement = f""" update unloze_racetimer_css.top5 t INNER JOIN ( SELECT {result}, ROW_NUMBER() OVER (ORDER BY {result} ASC) AS row_num FROM unloze_racetimer_css.zetimer_table_new WHERE {result} > 0 limit 5 ) t2 ON t.id = t2.row_num set t.{result} = t2.{result} where t.id = t2.row_num """ cur.execute(sql_statement) conn.commit() conn.close() if __name__ == '__main__': main()