Can't get history from deceased and deactivated patients

I am trying to modify the patient history search report to include inactivated and deceased patients. I can't work out why it doesn't find them.

Here is my query:

 select
  c.name as customer,
  c.description as description,
  p.name as patient,
  d1.value as dateOfBirth,
  ls.name as species,
  lb.name as breed,
  ad.value as notesOrProblems,
  part.activity_start_time as visitDate
from entities p
  join entity_relationships r on r.target_id = p.entity_id
  join entities c on r.source_id = c.entity_id
  join entity_details d1 on d1.entity_id = p.entity_id and d1.name = "dateOfBirth"
  join entity_details d2 on d2.entity_id = p.entity_id and d2.name = "species"
  join lookups ls on ls.code = d2.value and ls.arch_short_name = "lookup.species"
  join entity_details d3 on d3.entity_id = p.entity_id and d3.name = "breed"
  join lookups lb on lb.code = d3.value and lb.arch_short_name = "lookup.breed"
  join participations part ON part.`entity_id` = p.`entity_id`
  join `acts` acts ON acts.`act_id` = part.`act_id`
  join act_details ad ON ad.`act_id` = acts.`act_id`
where
  ad.name like "note" and
  ad.value like $P{HistorySearch} and
  ls.name like $P{species} and
  lb.name like $P{breed} and
  (part.activity_start_time between $P{startDate} and $P{finishDate})
order by c.name

Can anyone give me a clew as to why this query won't find them?

Many thanks,

Matt Y.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Can't get history from deceased and deactivated patients

If the patients don't have a breed, date of birth, or notes for the date range, then no results will be returned for them.

The following should solve the breed and date of birth issue.

If you want to find patients that have visits in the date range that may or may not have notes then the query is a little more complex.

 select
  c.name as customer,
  c.description as description,
  p.name as patient,
  d1.value as dateOfBirth,
  ls.name as species,
  lb.name as breed,
  ad.value as notesOrProblems,
  part.activity_start_time as visitDate
from entities p
  join entity_relationships r on r.target_id = p.entity_id
  join entities c on r.source_id = c.entity_id
  left join entity_details d1 on d1.entity_id = p.entity_id and d1.name = "dateOfBirth"
  join entity_details d2 on d2.entity_id = p.entity_id and d2.name = "species"
  join lookups ls on ls.code = d2.value and ls.arch_short_name = "lookup.species" and ls.name like $P{species}
  left join entity_details d3 on d3.entity_id = p.entity_id and d3.name = "breed"
  left join lookups lb on lb.code = d3.value and lb.arch_short_name = "lookup.breed" and lb.name like $P{breed}
  join participations part ON part.`entity_id` = p.`entity_id`
  join `acts` acts ON acts.`act_id` = part.`act_id` and acts.arch_short_name = "act.patientClinicalNote"
  join act_details ad ON ad.`act_id` = acts.`act_id` and ad.name ="note" and ad.value like $P{HistorySearch}
where (part.activity_start_time between $P{startDate} and $P{finishDate})
order by c.name 

Re: Can't get history from deceased and deactivated patients

Beat me too it.

Using arch_short_name when joining particpations (as Tim  has in his edits) and acts as a general rule. It will significantly speed queries and decrease workload on server.

Also the way Tim has added criteria to the JOIN rather then the WHERE can also speed up some queries.

Matt C

Syndicate content