| 286 | |
| 287 | |
| 288 | === Квери кое ги влече пациентите со слични дијагнози |
| 289 | |
| 290 | {{{ |
| 291 | with selected_person_diagnosis as( |
| 292 | select distinct d.diagnosis_id as diagnosis_id, d.short_description as label |
| 293 | from person p |
| 294 | join report r on r.person_id = p.person_id |
| 295 | join medicalreport_diagnosis mrd on mrd.report_id = r.report_id |
| 296 | join diagnosis d on mrd.diagnosis_id = d.diagnosis_id |
| 297 | where p.person_id = 14 --parameter (the most of them are person_id = 1) |
| 298 | ) |
| 299 | select cast(p2.person_id as bigint), |
| 300 | p2.name || ' ' || p2.surname as full_name, |
| 301 | cast(count(distinct spd.diagnosis_id) as bigint) as matching_diagnoses_count, |
| 302 | string_agg(distinct spd.label, ', ') as matching_labels |
| 303 | from selected_person_diagnosis spd |
| 304 | join medicalreport_diagnosis mrd2 on mrd2.diagnosis_id = spd.diagnosis_id |
| 305 | join report r2 on r2.report_id = mrd2.report_id |
| 306 | join person p2 on p2.person_id = r2.person_id |
| 307 | where p2.person_id != 14 --parameter of the person |
| 308 | group by p2.person_id, p2.name, p2.surname |
| 309 | having count(distinct spd.diagnosis_id) >=1 |
| 310 | order by matching_diagnoses_count desc; |
| 311 | }}} |