# AQL questions

**URL:** https://discourse.openehr.org/t/aql-questions/16615
**Category:** Technical (archive)
**Created:** [25 April 2019 12:17 UTC](https://discourse.openehr.org/t/aql-questions/16615 "2019-04-25T12:17:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://discourse.openehr.org/uploads/default/original/2X/f/f0a1dedb20c42747bddcafd6c7df9db5f34f003c.svg) [@system](https://discourse.openehr.org/u/system)
#### Post date: [25 April 2019 12:17 UTC](https://discourse.openehr.org/t/aql-questions/16615/1 "2019-04-25T12:17:53Z")

</div>

Hello,  
I have some problems concerning the formulation of an AQL query.  
I would like to check a laboratory analyte within a laboratory test. The analyte should have "Calcium" as analyte name and I would like to receive all EHRs that contain the analyte as the query result.  
The SELECT part seems easy, as I just want the EHRs, so this looks like this:

SELECT e

A problem I now have is about the nesting of the Archetypes. As the test is an OBSERVATION and an EHR contains only COMPOSITIONs, I wonder if I have to include a COMPOSITION in order to reach the tests. This would lead to a FROM part looking like this:

FROM EHR e  
CONTAINS COMPOSITION  
CONTAINS OBSERVATION b[openEHR-EHR-OBSERVATION.laboratory\_test\_result.v1]

The next problem I have is about the definition of the containment of the analyte in the FROM part or better in the WHERE part. The FROM part would require me to define the actual nesting structure of the test-OBSERVATION up to the analyte-CLUSTER. This would look like this:

FROM EHR e  
CONTAINS COMPOSITION  
CONTAINS OBSERVATION b[openEHR-EHR-OBSERVATION.laboratory\_test\_result.v1]  
CONTAINS HISTORY  
CONTAINS EVENT  
CONTAINS ITEM\_TREE  
CONTAINS CLUSTER c[openEHR-EHR-CLUSTER.laboratory\_test\_analyte.v1]

This looks a bit bloated, so I think this should rather be moved to the WHERE part, which would look like this:

FROM EHR e  
CONTAINS COMPOSITION  
CONTAINS OBSERVATION b[openEHR-EHR-OBSERVATION.laboratory\_test\_result.v1]  
WHERE b/data[at0001]/events[at0002]/data[at0003]/items[at0097]/items[at0024].value = 'Calcium'

In this WHERE path I am wondering about the necessity of the path part predicates (i.e. the [at...] parts). Are these predicates always necessary for every path part ? Or are they only necessary when the path part object would be ambiguous without the predicate (e.g. in the items of CLUSTERs or ITEM\_LISTs) ?  
Furthermore I wonder about the namespace from which the predicates are taken from. For the first part of the WHERE until the archetype slot is reached (i.e. b/data[at0001]/events[at0002]/data[at0003]/items[at0097]) the predicates are from the laboratory\_test\_result archetype. For the rest of the path (i.e. /items[at0024].value) the predicates are taken from the laboratory\_test\_analyte archetype. This switching of namespaces seems wrong, so I think I still have some misunderstanding of AQL concerning the predicates.

How would the correct query/queries look like ? Are there multiple valid alternatives to express my query using combinations of the approaches scetched above ?  
Greetings  
Georg

---

<div class="post-metadata">

### Author: ![system](https://discourse.openehr.org/uploads/default/original/2X/f/f0a1dedb20c42747bddcafd6c7df9db5f34f003c.svg) [@system](https://discourse.openehr.org/u/system)
#### Post date: [25 April 2019 14:47 UTC](https://discourse.openehr.org/t/aql-questions/16615/2 "2019-04-25T14:47:18Z")

</div>

Hi - great question.  
The answer is as always; it depends. The query below (similar to yours) will work also.  
What you have to consider when building AQLs is repeatable structures to make sure you hit the right instance. For this use-case I guess you would like any EHR which has a Calcium result. Thus you don’t care about which result is Calcium. But if you wanted to get the excact CLUSTER with Calcium combined with i.e. HISTORY.time you have to be a bit careful.

SELECT  
&nbsp;&nbsp;&nbsp;&nbsp;e  
FROM  
&nbsp;&nbsp;&nbsp;&nbsp;EHR e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONTAINS COMPOSITION c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONTAINS OBSERVATION o[openEHR-EHR-OBSERVATION.laboratory\_test.v1]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONTAINS CLUSTER c2[openEHR-EHR-CLUSTER.laboratory\_test\_panel.v1]  
WHERE  
&nbsp;&nbsp;&nbsp;&nbsp;c2/items[at0002]/items[at0001]/value/magnitude \> 123  
ORDER BY  
&nbsp;&nbsp;&nbsp;&nbsp;o/data[at0001]/events[at0002]/time DESC  
LIMIT 10

An even simpler AQL if you want any CLUSTER of a specific type is to only query for Composition with the CLUSTER. Like this:

SELECT  
&nbsp;&nbsp;&nbsp;&nbsp;e  
FROM  
&nbsp;&nbsp;&nbsp;&nbsp;EHR e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONTAINS COMPOSITION c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONTAINS CLUSTER c2[openEHR-EHR-CLUSTER.laboratory\_test\_panel.v1]  
LIMIT 10

Vennlig hilsen  
Bjørn Næss  
Produktansvarlig  
DIPS ASA

Mobil +47 93 43 29 10

---

<div class="post-metadata">

### Author: ![system](https://discourse.openehr.org/uploads/default/original/2X/f/f0a1dedb20c42747bddcafd6c7df9db5f34f003c.svg) [@system](https://discourse.openehr.org/u/system)
#### Post date: [25 April 2019 17:29 UTC](https://discourse.openehr.org/t/aql-questions/16615/3 "2019-04-25T17:29:40Z")

</div>

Hi Bjørn,  
Thank you for your answers. They make me assume that the CONTAINS-operator is recursive because in your second query you ommited the part with "CONTAINS OBSERVATION o[openEHR-EHR-OBSERVATION.laboratory\_test.v1]". Is this assumption correct ? This would make writing AQL a lot simpler.  
Another assumption I have is that the predicate namespaces always relate to the aliased archetype the path they are used within starts with. In your first query you use at0001 and at0002 each two times but they seem to have different meanings.  
Greetings  
Georg

---

<div class="post-metadata">

### Author: ![system](https://discourse.openehr.org/uploads/default/original/2X/f/f0a1dedb20c42747bddcafd6c7df9db5f34f003c.svg) [@system](https://discourse.openehr.org/u/system)
#### Post date: [25 April 2019 17:47 UTC](https://discourse.openehr.org/t/aql-questions/16615/4 "2019-04-25T17:47:45Z")

</div>

Georg,

CONTAINS is like the Xpath ‘//’ operator - it finds a match at any level of hierarchy (if there is one).

---

<div class="post-metadata">

### Author: ![system](https://discourse.openehr.org/uploads/default/original/2X/f/f0a1dedb20c42747bddcafd6c7df9db5f34f003c.svg) [@system](https://discourse.openehr.org/u/system)
#### Post date: [26 April 2019 07:43 UTC](https://discourse.openehr.org/t/aql-questions/16615/5 "2019-04-26T07:43:41Z")

</div>

Thomas answered the CONTAINS operator.

I am not sure how to best answer your second question. I will try the following 🙂

The node predicates as below:

\* c2/items[at0002]/items[at0001]/value/magnitude  
\* o/data[at0001]/events[at0002]/time

Here the "c2" and "o" is a reference to a path in the Composition, and the reference is defined in the FROM part of the AQL. The "c2" is synonymous with the following path:

"c2" == "/content[openEHR-EHR-OBSERVATION.laboratory\_test.v1]/data[at0001]/events[at0002]/data[at0003]/items[openEHR-EHR-CLUSTER.laboratory\_test\_panel.v1]"

Which gives "c2" + "path" to be:

"/content[openEHR-EHR-OBSERVATION.laboratory\_test.v1]/data[at0001]/events[at0002]/data[at0003]/items[openEHR-EHR-CLUSTER.laboratory\_test\_panel.v1]/ /items[at0002]/items[at0001]/value/magnitude"

Vennlig hilsen  
Bjørn Næss  
Produktansvarlig  
DIPS ASA

Mobil +47 93 43 29 10

---

<div class="post-metadata">

### Author: ![system](https://discourse.openehr.org/uploads/default/original/2X/f/f0a1dedb20c42747bddcafd6c7df9db5f34f003c.svg) [@system](https://discourse.openehr.org/u/system)
#### Post date: [30 April 2019 18:29 UTC](https://discourse.openehr.org/t/aql-questions/16615/6 "2019-04-30T18:29:47Z")

</div>

Ah, thanks for the clarifications.  
Greetings  
Georg
