# AQL - Querying compositions with multiple observations of the same type

**URL:** https://discourse.openehr.org/t/aql-querying-compositions-with-multiple-observations-of-the-same-type/4470
**Category:** Implementation
**Tags:** aql, ehrbase
**Created:** [19 September 2023 14:35 UTC](https://discourse.openehr.org/t/aql-querying-compositions-with-multiple-observations-of-the-same-type/4470 "2023-09-19T14:35:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_Avenue3](https://discourse.openehr.org/user_avatar/discourse.openehr.org/alex_avenue3/32/2991_2.png) [@Alex\_Avenue3](https://discourse.openehr.org/u/Alex_Avenue3)
#### Post date: [19 September 2023 14:35 UTC](https://discourse.openehr.org/t/aql-querying-compositions-with-multiple-observations-of-the-same-type/4470/1 "2023-09-19T14:35:27Z")

</div>

Hi all,

Long time reader, first time poster 🙂

Given a composition C that has multiple instances of the same observation archetype O, should I be able to query for all instances of O?

A trivial example: for the given ehr, there is one composition that contains two progress\_note observations.  
Using EHRBase and the query below, I only get one of the observations returned.

```auto
SELECT
  o
FROM EHR e
CONTAINS OBSERVATION o[openEHR-EHR-OBSERVATION.progress_note.v1]
WHERE e/ehr_id/value = $ehr_id

```

If I add a second composition with multiple notes, I get 2 results; one note form each composition.

Is this expected? If so, how/can my query be adjusted to return all progress notes in this example?

Thanks

---

<div class="post-metadata">

### Author: ![pablo](https://discourse.openehr.org/user_avatar/discourse.openehr.org/pablo/32/3505_2.png) [@pablo](https://discourse.openehr.org/u/pablo)
#### Post date: [19 September 2023 17:14 UTC](https://discourse.openehr.org/t/aql-querying-compositions-with-multiple-observations-of-the-same-type/4470/2 "2023-09-19T17:14:23Z")

</div>

Hi Alex, selecting observation and filtering by archetype id should return all the instances of the observations complying with that archetype, single or multiple inside the same compo.

---

<div class="post-metadata">

### Author: ![Seref](https://discourse.openehr.org/user_avatar/discourse.openehr.org/seref/32/13_2.png) [@Seref](https://discourse.openehr.org/u/Seref)
#### Post date: [19 September 2023 18:41 UTC](https://discourse.openehr.org/t/aql-querying-compositions-with-multiple-observations-of-the-same-type/4470/3 "2023-09-19T18:41:37Z")

</div>

Hi Alex,  
EhrBase is quite mature, so I too would expect your query to return two observations, given a composition with two observations.

There used to be a period during which EhrBase team were talking about implementing behaviour that would be more intuitive for users who may be less exposed to query languages such as SQL or XQuery etc.

AQL’s story is kind of complicated when it comes to how it behaves. I advocated for it to have a formal execution semantics for years (literally) but we could not get that effort off the ground (for good reasons). So as things stand, each vendor may or may not give you an explanation of how it functions, but these days most implementations converge in terms of results, except when they don’t 🙂

I still think the outcome should be what you’re expecting. If you’re able to perform a GET from the REST api and share the json form (or XML) of your compositions, we can speculate with a bit more confidence.

Re the history of the semantics discussion, these may make interesting (or boring )reading:  
[A discussion about Archetype Query Language semantics – Musings of a confused technology addict. (serefarikan.com)](https://serefarikan.com/2018/02/15/a-rough-and-inaccurate-guide-to-archetype-query-language-semantics/)

[Archetype Query Language, The Confusing Bits – Musings of a confused technology addict. (serefarikan.com)](https://serefarikan.com/2021/06/19/archetype-query-language-the-confusing-bits/)

[Archetype Query Language, The Confusing Bits, II – Musings of a confused technology addict. (serefarikan.com)](https://serefarikan.com/2021/06/29/archetype-query-language-the-confusing-bits-ii/)

[serefarikan/aql-discussion: examples in xquery and sparql to discuss aql semantics (github.com)](https://github.com/serefarikan/aql-discussion)

---

<div class="post-metadata">

### Author: ![birger.haarbrandt](https://discourse.openehr.org/user_avatar/discourse.openehr.org/birger.haarbrandt/32/24_2.png) [@birger.haarbrandt](https://discourse.openehr.org/u/birger.haarbrandt)
#### Post date: [20 September 2023 05:25 UTC](https://discourse.openehr.org/t/aql-querying-compositions-with-multiple-observations-of-the-same-type/4470/4 "2023-09-20T05:25:43Z")

</div>

Hi all,

thank you for your kind words @Seref, but in this case, it is a known bug ([AQL Not returning multiple diagnosis from one composition · Issue #1019 · ehrbase/ehrbase · GitHub](https://github.com/ehrbase/ehrbase/issues/1019)). The team is currently working on a revision of EHRbase’s AQL engine which will address this behavior amongst others. Once this is done, EHRbase will also finally be released as 1.0.

---

<div class="post-metadata">

### Author: ![Alex\_Avenue3](https://discourse.openehr.org/user_avatar/discourse.openehr.org/alex_avenue3/32/2991_2.png) [@Alex\_Avenue3](https://discourse.openehr.org/u/Alex_Avenue3)
#### Post date: [20 September 2023 08:23 UTC](https://discourse.openehr.org/t/aql-querying-compositions-with-multiple-observations-of-the-same-type/4470/5 "2023-09-20T08:23:53Z")

</div>

Thank you @pablo @Seref @birger.haarbrandt

---

<div class="post-metadata">

### Author: ![surfer](https://discourse.openehr.org/user_avatar/discourse.openehr.org/surfer/32/2326_2.png) [@surfer](https://discourse.openehr.org/u/surfer)
#### Post date: [7 March 2024 15:38 UTC](https://discourse.openehr.org/t/aql-querying-compositions-with-multiple-observations-of-the-same-type/4470/6 "2024-03-07T15:38:15Z")

</div>

Updates on this problem? Is the solution about to be released? if not, do you have an idea of what the expected timelines could be?
