import fdb, datetime

conn = fdb.connect(
    dsn=r'localhost:c:\medicus 3\data\medicus.fdb',
    user='SYSDBA', password='masterkey', charset='win1250')

cur = conn.cursor()
dnes = datetime.date.today().isoformat()

cur.execute("""
    SELECT COUNT(*) FROM KAR
    WHERE (vyrazen = 'N')
    AND EXISTS (
        SELECT id FROM registr r
        JOIN icp i ON r.idicp = i.idicp
        WHERE r.idpac = kar.idpac
          AND (r.datum <= ?)
          AND (r.datum_zruseni IS NULL OR r.datum_zruseni >= ?)
          AND (r.priznak IN ('V','D','A'))
          AND (i.icp = '09305001')
          AND (i.odb = '001')
    )
""", (dnes, dnes))

pocet = cur.fetchone()[0]
print(f'Registrovaných pacientů: {pocet}')
conn.close()
