73 | | == Просечна возраст на пациенти за во даден оддел |
| 73 | == Просечна возраст на пациенти за во даден оддел за претходната година |
| 74 | |
| 75 | |
| 76 | {{{#!sql |
| 77 | select oo.bolnica_id, oo.oddel_id, |
| 78 | ( |
| 79 | select avg(distinct extract (year from age(((case when substring(c.embg, 5, 1) in ('9') |
| 80 | then '1'|| substring(c.embg, 5, 3) else '2'|| substring(c.embg, 5, 3) end) |
| 81 | || '-' || substring(c.embg, 3, 2) || '-' || substring(c.embg, 1, 2))::date))) as godini |
| 82 | from oddel o |
| 83 | join bolnica b on o.bolnica_id = b.bolnica_id |
| 84 | join upat u on (o.bolnica_id, o.oddel_id) = (u.bolnica_id, u.oddel_id) |
| 85 | join rezervacija r on u.upat_id = r.upat_id |
| 86 | join termin t on (r.doktor_id, r.termin_id) = (t.doktor_id, t.termin_id) |
| 87 | join pacient p on u.covek_pacient_id = p.covek_pacient_id |
| 88 | join covek c on p.covek_pacient_id = c.covek_id |
| 89 | where extract (year from t.vreme) = extract (year from now() - interval '1 year') |
| 90 | group by o.bolnica_id, o.oddel_id |
| 91 | having (o.bolnica_id, o.oddel_id) = (oo.bolnica_id, oo.oddel_id) |
| 92 | ) prosek_godini_pacienti |
| 93 | from oddel oo |
| 94 | }}} |
| 95 | |