Every encounter carries one or more ICD-10-CM codes, stored in hd_diagnoses with a seq number. seq 1 is the principal diagnosis of an inpatient stay (the condition chiefly responsible for the admission) or the first-listed diagnosis of a visit. The rest are secondary. Rows are not always listed in seq order, so read seq, not position.
An ICD-10-CM code starts with a letter. The first three characters are the category, a dot follows them, and up to four more characters add detail: I50 is heart failure, and I50.23 is acute on chronic systolic heart failure. The first letter places a code in a chapter (I for the circulatory system, S for injuries). Injury codes end in a seventh character: the A in S72.001A means initial encounter, while the injury is being actively treated.
Chronic conditions are coded again at every visit, so a code repeats across encounters. To count a patient's conditions, count distinct codes; to count visits with a condition, count encounters.
Category, dot, detail.