# Querying more than one template (JOIN) **Category:** [AQL](https://discourse.openehr.org/c/aql/43) **Created:** 2023-03-27 08:27 UTC **Views:** 653 **Replies:** 12 **URL:** https://discourse.openehr.org/t/querying-more-than-one-template-join/3751 --- ## Post #1 by @CzarMich Probably this is an overkill or there is somebody out there who has faced the same issue. I am trying to perform a simple "join" (as per SQL terms) to count patients that are contained in the 3 different templates (different archetypes), but I cannot seem to get a response. I am not sure of the behaviour hence seeking for help here, hopefully someone can guide me a. However, if I try the count on individual templates, I do get the desired result. I am using Better openEHR Platform. ``` SELECT COUNT(DISTINCT e/ehr_status/subject/external_ref/id/value) as Patient_Numbers FROM EHR e CONTAINS COMPOSITION d[openEHR-EHR-COMPOSITION.personendaten.v0] CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.progress_note.v0] CONTAINS COMPOSITION f[openEHR-EHR-COMPOSITION.report.v1] WHERE d/name/value='Person' AND c/archetype_details/template_id/value='Follow-up' AND f/archetype_details/template_id/value='Echokardiographie' OFFSET 0 LIMIT 5 ``` Thank you. --- ## Post #2 by @ian.mcnicoll ``` SELECT COUNT(DISTINCT e/ehr_status/subject/external_ref/id/value) as Patient_Numbers FROM EHR e CONTAINS COMPOSITION d[openEHR-EHR-COMPOSITION.personendaten.v0] AND CONTAINS COMPOSITION c[openEHR-EHR-OR COMPOSITION.progress_note.v0] AND CONTAINS COMPOSITION f[openEHR-EHR-COMPOSITION.report.v1] WHERE d/name/value='Person' AND c/archetype_details/template_id/value='Follow-up' AND f/archetype_details/template_id/value='Echokardiographie' ``` Try adding the 'and' clauses as the current AQL implies Nesting of contains which is actually impossible. Having said that, I' m not sure you really need to be 'joining' this way - what is the actual question you are trying to answer? --- ## Post #3 by @CzarMich Thanks @ian.mcnicoll . Well, my actual question/request is how can I count the number of subjects (Patient_numbers) that have compositions created based on 3 different templates. I have 3 templates; demographics (Person), and the other 2 (Reports). Possible that I have (Template 1)350 Subjects; but (Template 2) 40 progress_notes and (Template 3) 30 reports. So to get the right count numbers for the scenario described above, I need to do something related to "nesting" or joining I am assuming. I hope this clarifies my question. Will be glad for any help or guidance. Thanks, Michael --- ## Post #4 by @ian.mcnicoll You could also try ``` CONTAINS (COMPOSITION d[openEHR-EHR-COMPOSITION.personendaten.v0] AND COMPOSITION c[openEHR-EHR-OR COMPOSITION.progress_note.v0] AND COMPOSITION f[openEHR-EHR-COMPOSITION.report.v1]) ``` --- ## Post #5 by @CzarMich Unfortunately this results into "Query is not valid" ``` SELECT COUNT(DISTINCT e/ehr_status/subject/external_ref/id/value) as Patient_Numbers FROM EHR e CONTAINS (COMPOSITION d[openEHR-EHR-COMPOSITION.personendaten.v0] AND COMPOSITION c[openEHR-EHR-OR COMPOSITION.progress_note.v0] AND COMPOSITION f[openEHR-EHR-COMPOSITION.report.v1]) WHERE d/archetype_details/template_id/value='Person' AND c/archetype_details/template_id/value='Follow-up' AND f/archetype_details/template_id/value='Echokardiographie' ``` --- ## Post #6 by @CzarMich I tried to "simplify" the query with just two templates and using 'value IN' but still the nesting is not working out. probably my syntax is wrong. Will be glad for any suggestions. `SELECT COUNT(DISTINCT e/ehr_status/subject/external_ref/id/value) as Patient_Numbers FROM EHR e CONTAINS COMPOSITION d[openEHR-EHR-COMPOSITION.personendaten.v0] WHERE e/ehr_status/subject/external_ref/id/value IN (SELECT (DISTINCT e/ehr_status/subject/external_ref/id/value) FROM EHR e CONTAINS COMPOSITION c[openEHR-EHR-OR COMPOSITION.progress_note.v0] WHERE c/archetype_details/template_id/value='Follow-up') AND d/archetype_details/template_id/value='Person'` --- ## Post #7 by @ian.mcnicoll [quote="ian.mcnicoll, post:2, topic:3751"] `WHERE d/name/value='Person' AND c/archetype_details/template_id/value='Follow-up' AND f/archetype_details/template_id/value='Echokardiographie'` [/quote] This is working for me on BetterCDR ``` SELECT COUNT(DISTINCT e/ehr_status/subject/external_ref/id/value) as Patient_Numbers FROM EHR e CONTAINS (COMPOSITION a[openEHR-EHR-COMPOSITION.adverse_reaction_list.v1] and COMPOSITION m[openEHR-EHR-COMPOSITION.medication_list.v0]) WHERE a/name/value='Adverse reaction list' and m/name/value='Medication statement list' ``` --- ## Post #8 by @CzarMich [quote="CzarMich, post:5, topic:3751"] `c/archetype_details/template_id/value='Follow-up'` [/quote] Yes, you are right. With 2 Templates it works. So for now I will just stick to querying with the two templates. Thanks for the help. ``` ` SELECT COUNT(DISTINCT e/ehr_status/subject/external_ref/id/value) as Patient_Numbers FROM EHR e CONTAINS (COMPOSITION d[openEHR-EHR-COMPOSITION.personendaten.v0] AND COMPOSITION f[openEHR-EHR-COMPOSITION.report.v1]) WHERE d/archetype_details/template_id/value='Person' AND f/archetype_details/template_id/value='Echokardiographie'` ``` --- ## Post #9 by @ian.mcnicoll I'd expect it to work with more than 2 templates --- ## Post #10 by @CzarMich Yes, it does. One of my template just had issues. Thanks a bunch @ian.mcnicoll --- ## Post #11 by @ian.mcnicoll [quote="CzarMich, post:10, topic:3751"] One of my template just had issues. [/quote] Welcome to my world!! Glad you got it working!! --- ## Post #12 by @pablo [quote="CzarMich, post:10, topic:3751"] Yes, it does. One of my template just had issues. [/quote] Was that issue related with the template syntax or semantics? just curious. --- ## Post #13 by @CzarMich [quote="CzarMich, post:8, topic:3751"] `d/archetype_details/template_id/value='Person'` [/quote] Hi Pablo, It was a simple syntax issue in the naming. --- **Canonical:** https://discourse.openehr.org/t/querying-more-than-one-template-join/3751 **Original content:** https://discourse.openehr.org/t/querying-more-than-one-template-join/3751