# Queries on multiple Evaluations in a Composition **Category:** [Implementation](https://discourse.openehr.org/c/implem/39) **Created:** 2024-02-06 11:45 UTC **Views:** 366 **Replies:** 9 **URL:** https://discourse.openehr.org/t/queries-on-multiple-evaluations-in-a-composition/4910 --- ## Post #1 by @Samuel_elmeas We are using a template where there are multiple `evaluations` of the same type (unbound amount) within a single `composition`. To be more specific, we have an arbitrary amount of diagnosis, using the Problem/Diagnosis Evaluation archetype, within a template that's used for the corresponding form. The problem arises in a separate part of the application where we want to show the list of all diagnosis (just the name of each). That means we need to query a field within each evaluation that has been added to the composition. It currently seems that the only way to get this data is to retrieve the entire composition using and then handle the parsing ourselves. Are we missing anything here? Can this somehow be done with AQL? --- ## Post #2 by @ian.mcnicoll That is exactly what AQL is for.. 1. This will return an array of problem_diagnosis objects for the selected ehr_id /value ``` SELECT g AS problem_diagnosis FROM EHR e [ehr_id/value = '{{ehrId}}'] CONTAINS COMPOSITION c CONTAINS EVALUATION g[openEHR-EHR-EVALUATION.problem_diagnosis.v1] ``` You can actually go right down to leaf node. ``` SELECT g/data[at0001]/items[at0002]/value/value AS Problem_Diagnosis_name, g/data[at0001]/items[at0003]/value AS Date_time_clinically_recognised FROM EHR e [ehr_id/value = '{{ehrId}}'] CONTAINS COMPOSITION c CONTAINS EVALUATION g[openEHR-EHR-EVALUATION.problem_diagnosis.v1] ``` but you might start to find duplicates rows being created with more complex queries ( the Cartesian product problem) if the nodes selected are multiple occurence. --- ## Post #3 by @ian.mcnicoll BTW this has been setup to find problem/diagnosis entries in **any** composition. You might want to put more constraints in to control exactly where the AQL should find your 'master problem list'. e.g. ```sql SELECT m/data[at0001]/items[at0002]/value AS problem_diagnosis_name, m/data[at0001]/items[at0003]/value AS Date_time_clinically_recognised FROM EHR e[ehr_id/value = '{{ehrId}}'] CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.problem_list.v1] CONTAINS EVALUATION m[openEHR-EHR-EVALUATION.problem_diagnosis.v1] ``` or ```sql SELECT m/data[at0001]/items[at0002]/value AS problem_diagnosis_name, m/data[at0001]/items[at0003]/value AS Date_time_clinically_recognised FROM EHR e[ehr_id/value = '{{ehrId}}'] CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.problem_list.v1] CONTAINS EVALUATION m[openEHR-EHR-EVALUATION.problem_diagnosis.v1] WHERE c/archetype_details/template_id/value = 'My Template Id' ``` --- ## Post #4 by @DavidD @ian.mcnicoll Thanks for the quick response! I have the following query: ``` SELECT c/uid/value, g/data[at0001]/items[at0002]/value/value AS problem_diagnosis FROM EHR e [ehr_id/value = ''] CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.problem_list-MVP_diagnosis_list.v2] CONTAINS EVALUATION g[openEHR-EHR-EVALUATION.problem_diagnosis.v1] WHERE c/archetype_details/template_id/value='