# The challenge of querying different levels of data at the same time

**URL:** https://discourse.openehr.org/t/the-challenge-of-querying-different-levels-of-data-at-the-same-time/5685
**Category:** AQL
**Tags:** aql
**Created:** [10 September 2024 03:39 UTC](https://discourse.openehr.org/t/the-challenge-of-querying-different-levels-of-data-at-the-same-time/5685 "2024-09-10T03:39:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![linforest](https://discourse.openehr.org/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@linforest](https://discourse.openehr.org/u/linforest)
#### Post date: [10 September 2024 03:39 UTC](https://discourse.openehr.org/t/the-challenge-of-querying-different-levels-of-data-at-the-same-time/5685/1 "2024-09-10T03:39:49Z")

</div>

Because the template is assembled using slots in multiple _Archetypes_, the data that should be _conceptually/logically_ at the same level is _actually/structurally_ at different levels, which makes it challenging to query the data at the same time (OR in a single ADL query/subquery), especially for those users who do not know the tree structure of the target template beforehand.

 ![image](https://discourse.openehr.org/uploads/default/original/2X/4/40f5fb6a208102ff09d7fd95bfb9414ddcc74974.png)

**Related Topic** :

- [Physical activity archetypes; exercise, steps etc from apps & devices](https://discourse.openehr.org/t/physical-activity-archetypes-exercise-steps-etc-from-apps-devices/983)

---

<div class="post-metadata">

### Author: ![sebastian.iancu](https://discourse.openehr.org/user_avatar/discourse.openehr.org/sebastian.iancu/32/31_2.png) [@sebastian.iancu](https://discourse.openehr.org/u/sebastian.iancu)
#### Post date: [10 September 2024 07:33 UTC](https://discourse.openehr.org/t/the-challenge-of-querying-different-levels-of-data-at-the-same-time/5685/2 "2024-09-10T07:33:37Z")

</div>

Hi,  
It is not very clear from your post what is challenging/difficult. Perhaps try to explain more on what did with examples, or what you intended to query or find out, how was therefore the AQL query looking, what was your (initial) expectation on writing the query (especially what was challenging), etc.

---

<div class="post-metadata">

### Author: ![linforest](https://discourse.openehr.org/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@linforest](https://discourse.openehr.org/u/linforest)
#### Post date: [10 September 2024 08:24 UTC](https://discourse.openehr.org/t/the-challenge-of-querying-different-levels-of-data-at-the-same-time/5685/3 "2024-09-10T08:24:31Z")

</div>

Hi @sebastian.iancu. Excuse me for my vague (even for myself) expression. Rethinking it carefully, what I wanted to express might be “that _would_ be nice if the two groups of data points could be queried in a single ADL query using the same path”. Actually, maybe it’s not possible to write such an ADL query without specifying their full paths, just like the double slash (//) in an XPath expression “bookstore//book” which omits the _middle/intermediate paths_.

I don’t know if I made it clear this time?  
Thanks again.

---

<div class="post-metadata">

### Author: ![damoca](https://discourse.openehr.org/user_avatar/discourse.openehr.org/damoca/32/2606_2.png) [@damoca](https://discourse.openehr.org/u/damoca)
#### Post date: [10 September 2024 12:18 UTC](https://discourse.openehr.org/t/the-challenge-of-querying-different-levels-of-data-at-the-same-time/5685/4 "2024-09-10T12:18:09Z")

</div>

You can get similar results by retrieving all the ELEMENTs in AQL, independently of their path.

(I don’t know the exact archetype identifier in the example below)

```auto
SELECT elem
FROM EHR e
CONTAINS COMPOSITION c
   CONTAINS OBSERVATION o[openehr-EHR-OBSERVATION.Physical_activity.v1]
      CONTAINS ELEMENT elem

```

---

<div class="post-metadata">

### Author: ![sebastian.iancu](https://discourse.openehr.org/user_avatar/discourse.openehr.org/sebastian.iancu/32/31_2.png) [@sebastian.iancu](https://discourse.openehr.org/u/sebastian.iancu)
#### Post date: [10 September 2024 13:33 UTC](https://discourse.openehr.org/t/the-challenge-of-querying-different-levels-of-data-at-the-same-time/5685/5 "2024-09-10T13:33:27Z")

</div>

Indeed, the `... CONTAINS bookstore CONTAINS book` is a search that in xPath would be equivalent to `booskstore//book` (so a ‘descendant’ relationship), and assuming all what you were looking is a `book`type, regardless the relationship level (child, grandchild, etc), then you will get what you are looking for.

---

<div class="post-metadata">

### Author: ![linforest](https://discourse.openehr.org/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@linforest](https://discourse.openehr.org/u/linforest)
#### Post date: [10 September 2024 19:57 UTC](https://discourse.openehr.org/t/the-challenge-of-querying-different-levels-of-data-at-the-same-time/5685/6 "2024-09-10T19:57:27Z")

</div>

Ah, it’s just what I want. And I can hardly wait to try this query… Thanks a lot, @damoca.

> [@damoca](#):
>
> ```auto
> SELECT elem
> FROM EHR e
> CONTAINS COMPOSITION c
> CONTAINS OBSERVATION o[openehr-EHR-OBSERVATION.Physical_activity.v1]
> CONTAINS ELEMENT elem
> 
> ```

This query works! Agian, thanks, @damoca.

---

<div class="post-metadata">

### Author: ![linforest](https://discourse.openehr.org/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@linforest](https://discourse.openehr.org/u/linforest)
#### Post date: [10 September 2024 20:02 UTC](https://discourse.openehr.org/t/the-challenge-of-querying-different-levels-of-data-at-the-same-time/5685/7 "2024-09-10T20:02:40Z")

</div>

@sebastian.iancu Thank you for your clear and unambiguous description.
