Querying an openEHR CDR with SQL, Cypher, and GraphQL? Yes, it’s possible.

I found a similar structure in COVID-19 Pneumonia Diagnosis and Treatment (7th edition).opt and thanks to @pablo instance generator, I have a composition instance to use.

It uses medication_order.v2 instead of v3 but it probably doesn’t mater for this exercise.

I can generate some for you tomorrow :+1:

Hi @siljelb quick side question from checking your queries: it seems you are versioning OPT 1.4 ids, is that correct?

There is a whole discussion, now it seems finally settled for HRID, though far from being implemented about template_id and how to version them.

Can you briefly describe which versioning rules are you applying for templates?

Thanks!

I couldn’t wait for the data instances for your example @siljelb so I adapted the 1st example to the COVID-19 OPT.

What do you think?

I’m not saying AQL is wrong. I’m just experimenting with more widely available SQL and all the tooling already available. By using SQL, I was able to write a working CDR which can be queried with SQL. I’m not sure how much time it took to build other CDRs for this feature set, but I’m sure it was more than 3 weeks :wink:

SELECT $uid, 
       $medication_order.activities.description.items, 
       $health_risk.data.items.value.value
FROM   `openEHR-EHR-COMPOSITION.encounter.v1`
LET    $adhoc = rel(`openEHR-EHR-SECTION.adhoc.v1`),
       $medication_order = $adhoc.rel(`openEHR-EHR-INSTRUCTION.medication_order.v2`),
       $health_risk = $adhoc.rel(`openEHR-EHR-EVALUATION.health_risk.v1`)
WHERE  archetype_details.template_id.value LIKE 'COVID%'
AND    $medication_order CONTAINS ( activities CONTAINS ( 
           archetype_node_id = 'at0001' AND
           description.archetype_node_id = 'at0002' AND
           description.items CONTAINS (
               archetype_node_id = 'at0070' AND
               name.value= 'Medication item' AND
               value.defining_code.code_string = '45754009'
           )
       ))
AND    $health_risk CONTAINS ( data.items CONTAINS (
           value.value = 'Fit for discharge'
       ))

@siljelb I’m still eager to try my luck with the AQLs you posted if you can provide the OPT or a data instance.