First() and Last() functions

, ,

List class in openEHR has the functions first() and last()
Are those functions supported by any of the AQL engines out there? Any examples?
In general, can we access to specific objects inside of a returned List? (such as pd/data[at0001]/items[at0002, 1] or pd/data[at0001]/items[at0002][1] or so on)

Typical Stackoverflow reply: “Why would you even want that?” :wink:

1 Like

It’s actually a query to see if a patient has multiple conditions or just one, and a easy way of checking that is to see if exists problem[2]
Was asking for first last to check where first()!=last()

I am not sure how such an operation could be interpreted by an AQL engine. It will, IMHO, be very contextual depending on what the intention of the AQL editor (person/client).

Did you expect to use it in the SELECT or WHERE part?

Is it to evaluate i.e all problem/diagnosis instances which have more than one repeated statement of some specific data?

I would use a specific query and then look at the number of results, og use the COUNT function to achieve something like this. Wouldn’t that be good enough?

In openEHR we have adhoc means to filter lists by label (e.g. in SECTIONs), so I was also wondering if that was being overridden in any implementation for a more general selection use case.
I don’t really see using count as a substitute as that would need be in the WHERE (SELECT ehr_id FROM C WHERE count(X)>=2), so it would need a subquery or process it outside of AQL. The selection I can see being used in both SELECT (see SECTION above, as I think we can return a specific SECTION in SELECT) or in WHERE (again, we do that for SECTION already). First and Last as just two shorthand syntax for that same use.
For this use case we want the patients, so these would go into the WHERE, but specifically last() I can see being used in the SELECT part.
In general we have a very inconsistent use of functions in openEHR, some are treated as almost attributes (being able to even create constraints on them) while others are just ignored for what I see.

I could rename this thread “accessing a list in AQL” if makes anyone happier

By the way, doing this on both XPath or Json is pretty trivial, last function is actually supported on xpath (//path/items[last()]), and you have access to length in json so it would end it as path.items[path.item.length-1]

For first(), xpath uses 1 as index and json 0, but the implementation is trivial

Doing it on one instance level (as in xpath) is trivial. Doing it across all possible records is another beast IMHO :wink:

All these work at instance level, the presented functions are on the RM, not AQL