| 93 | == Koja e најновата терапија според датум која е препишена, за која дијагноза е препишена, и во кој ветеринарен центар е препишена. |
| 94 | {{{ |
| 95 | WITH LatestTherapies AS ( |
| 96 | SELECT |
| 97 | ptvc.id_pets, |
| 98 | MAX(t.appoitmentDate) AS latest_therapy_date |
| 99 | FROM |
| 100 | project.pets_visit_vet_centers ptvc |
| 101 | JOIN |
| 102 | project.therapy t ON ptvc.id_pets = t.diagnosticsID |
| 103 | GROUP BY |
| 104 | ptvc.id_pets |
| 105 | ) |
| 106 | |
| 107 | SELECT |
| 108 | vc.name AS vet_center_name, |
| 109 | p.id AS pet_id, |
| 110 | --p.description AS pet_description, |
| 111 | d.description AS diagnosis_description, |
| 112 | t.description AS latest_therapy_description, |
| 113 | lt.latest_therapy_date |
| 114 | FROM |
| 115 | project.vet_centers vc |
| 116 | JOIN |
| 117 | project.pets_visit_vet_centers ptvc ON vc.id = ptvc.id_vet_centers |
| 118 | JOIN |
| 119 | project.pets p ON ptvc.id_pets = p.id |
| 120 | JOIN |
| 121 | project.diagnostics d ON p.id = d.usersID |
| 122 | LEFT JOIN |
| 123 | LatestTherapies lt ON p.id = lt.id_pets |
| 124 | LEFT JOIN |
| 125 | project.therapy t ON lt.latest_therapy_date = t.appoitmentDate AND p.id = t.diagnosticsID |
| 126 | ORDER BY |
| 127 | latest_therapy_date DESC; |
| 128 | }}} |