import pymysql

mysql = pymysql.connect(
    host="192.168.1.76",
    port=3307,
    user="root",
    password="Vlado9674+",
    database="medevio",
    charset="utf8mb4",
    cursorclass=pymysql.cursors.DictCursor
)

sql = """
SELECT rc
FROM (
    SELECT rc, stav,
           ROW_NUMBER() OVER (PARTITION BY rc ORDER BY k_datu DESC) AS rn
    FROM vzp_stav_pojisteni
) t
WHERE rn = 1
  AND stav <> '1'
"""

with mysql.cursor() as cur:
    cur.execute(sql)
    rows = cur.fetchall()

# výsledná množina rodných čísel
rc_not_insured = {row["rc"] for row in rows}

mysql.close()

print(f"Nalezeno {len(rc_not_insured)} nepojištěných pacientů")
# print(rc_not_insured)  # volitelně
