Clinical Use

AQL Query Guide

AMCDR AQL patterns for patient lookup, template queries, multi-composition logic, aggregation, HAVING, saved reports, and operational cohorts.

When To Use AQL

Use AQL to query stored openEHR records across patients: lists, cohorts, extracts, duplicate checks, worklists, and saved reports. For an individual patient’s clinical review, use the patient workspace.

Patient detail

Prefer the patient workspace, forms, and composition viewer.

Many patients

Use AQL with filters, aliases, grouping, and exports.

Reusable reports

Save tested AQL with stable aliases and parameters.

Protected records

AQL results are still controlled by RBAC, scope, and protected-record filtering.

Basic Shape

An AQL query selects paths, declares an EHR/composition source, filters rows, and optionally groups, sorts, and limits output.

SELECT
  c/name/value AS composition_name,
  c/archetype_details/template_id/value AS template_id,
  c/context/start_time/value AS recorded_at
FROM EHR e
CONTAINS COMPOSITION c
WHERE c/name/value EXISTS
ORDER BY recorded_at DESC
LIMIT 50
PartPurposeExample
SELECTChoose result columns and aliases.c/name/value AS composition_name
FROMChoose the openEHR root and containment.FROM EHR e CONTAINS COMPOSITION c
WHEREFilter candidate rows before grouping.c/name/value = 'Vital signs'
GROUP BYBuild aggregate groups.GROUP BY c/archetype_details/template_id/value
HAVINGFilter aggregate groups.HAVING COUNT(*) >= 2
ORDER BYSort final rows.ORDER BY recorded_at DESC

Aliases And Paths

Aliases are names assigned in the FROM clause. They are not hardcoded. Use standard identifiers such as c, obs, lab, person, or echo_report. Do not use dot aliases such as g.h; openEHR paths use slash separators.

SELECT
  lab/name/value AS lab_section,
  result/name/value AS result_name
FROM EHR e
CONTAINS COMPOSITION lab[openEHR-EHR-COMPOSITION.report.v1]
CONTAINS OBSERVATION result[openEHR-EHR-OBSERVATION.laboratory_test_result.v1]
WHERE result/name/value EXISTS

The alias is the first part of the path. In the example above, lab/... points to the composition and result/... points to the contained observation.

Source Patterns

All EHRs

Use this when you need one row per EHR or patient identity reference.

SELECT
  e/ehr_id/value AS ehr_id,
  e/ehr_status/subject/external_ref/id/value AS patient_id
FROM EHR e
LIMIT 100

All Compositions

Use this for a broad composition inventory or data-quality check.

SELECT
  e/ehr_id/value AS ehr_id,
  c/uid/value AS composition_uid,
  c/name/value AS composition_name,
  c/archetype_details/template_id/value AS template_id
FROM EHR e
CONTAINS COMPOSITION c
ORDER BY c/context/start_time/value DESC
LIMIT 100

One Template

Put the archetype or template constraint directly on the composition source when the query is about one known template family.

SELECT
  e/ehr_status/subject/external_ref/id/value AS patient_id,
  c/uid/value AS composition_uid,
  c/context/start_time/value AS recorded_at
FROM EHR e
CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.report.v1]
WHERE c/archetype_details/template_id/value = 'Echokardiographie'
ORDER BY recorded_at DESC

Version-Aware Query

Use VERSION when the report needs version lifecycle or commit audit information.

SELECT
  v/uid/value AS version_uid,
  v/lifecycle_state/value AS lifecycle_state,
  v/commit_audit/time_committed/value AS committed_at,
  c/name/value AS composition_name
FROM EHR e
CONTAINS VERSION v
CONTAINS COMPOSITION c
WHERE v/lifecycle_state/value EXISTS
ORDER BY committed_at DESC

Single Patient Versus Population Queries

One Patient By Patient Identifier

Use the EHR subject external reference when the caller has the clinical patient ID.

SELECT
  c/name/value AS composition_name,
  c/archetype_details/template_id/value AS template_id,
  c/context/start_time/value AS recorded_at
FROM EHR e
CONTAINS COMPOSITION c
WHERE e/ehr_status/subject/external_ref/id/value = $patient_id
ORDER BY recorded_at DESC

One Patient By EHR ID

Use the EHR identifier when another openEHR system or adapter has already resolved the EHR.

SELECT
  c/uid/value AS composition_uid,
  c/name/value AS composition_name
FROM EHR e
CONTAINS COMPOSITION c
WHERE e/ehr_id/value = $ehr_id

All Patients With A Template

This is a population query. It returns every patient that has at least one matching composition.

SELECT DISTINCT
  e/ehr_status/subject/external_ref/id/value AS patient_id
FROM EHR e
CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.adverse_reaction_list.v1]
WHERE c/name/value = 'Adverse reaction list'

Filtering Examples

Equality And Numeric Comparisons

SELECT
  e/ehr_status/subject/external_ref/id/value AS patient_id,
  obs/data[at0002]/events[at0003]/data[at0001]/items[at0004]/value/magnitude AS temperature_c
FROM EHR e
CONTAINS COMPOSITION c
CONTAINS OBSERVATION obs[openEHR-EHR-OBSERVATION.body_temperature.v2]
WHERE obs/data[at0002]/events[at0003]/data[at0001]/items[at0004]/value/magnitude >= 38

EXISTS And NOT EXISTS

SELECT
  c/uid/value AS composition_uid,
  c/name/value AS composition_name
FROM EHR e
CONTAINS COMPOSITION c
WHERE EXISTS c/feeder_audit/originating_system_item_ids/id
AND NOT EXISTS c/feeder_audit/feeder_system_item_ids/id

IN, NOT IN, LIKE, MATCHES, BETWEEN, NULL

SELECT
  e/ehr_status/subject/external_ref/id/value AS patient_id,
  c/name/value AS composition_name,
  c/category/defining_code/code_string AS category_code
FROM EHR e
CONTAINS COMPOSITION c
WHERE c/category/defining_code/code_string IN ('433', '431')
AND c/name/value LIKE 'Discharge%'
AND c/context/start_time/value BETWEEN '2026-01-01' AND '2026-12-31'
AND c/archetype_details/template_id/value IS NOT NULL

Use MATCHES {'a', 'b'} when authoring openEHR-style set matching. Use IN (...) when writing SQL-like lists for AMCDR report builders.

Multiple Compositions And Templates

openEHR AQL does not use SQL JOIN. To require multiple compositions for the same EHR, use grouped containment. AMCDR binds each alias across compositions belonging to the same EHR.

Patients With Two Required Templates

SELECT COUNT(DISTINCT e/ehr_status/subject/external_ref/id/value) AS patient_count
FROM EHR e
CONTAINS (
  COMPOSITION adverse[openEHR-EHR-COMPOSITION.adverse_reaction_list.v1]
  AND COMPOSITION meds[openEHR-EHR-COMPOSITION.medication_list.v0]
)
WHERE adverse/name/value = 'Adverse reaction list'
AND meds/name/value = 'Medication statement list'

Patients With Demographics And Echo Report

SELECT COUNT(DISTINCT e/ehr_status/subject/external_ref/id/value) AS patient_count
FROM EHR e
CONTAINS (
  COMPOSITION person[openEHR-EHR-COMPOSITION.personendaten.v0]
  AND COMPOSITION echo[openEHR-EHR-COMPOSITION.report.v1]
)
WHERE person/archetype_details/template_id/value = 'Person'
AND echo/archetype_details/template_id/value = 'Echokardiographie'

Either Of Two Templates

SELECT DISTINCT
  e/ehr_status/subject/external_ref/id/value AS patient_id
FROM EHR e
CONTAINS (
  COMPOSITION discharge[openEHR-EHR-COMPOSITION.report.v1]
  OR COMPOSITION death_notice[openEHR-EHR-COMPOSITION.report.v1]
)
WHERE discharge/name/value = 'Discharge summary'
OR death_notice/name/value = 'Death notification'

Nested Containment Inside A Template

SELECT
  e/ehr_status/subject/external_ref/id/value AS patient_id,
  bp/name/value AS observation_name,
  systolic/value/magnitude AS systolic
FROM EHR e
CONTAINS COMPOSITION encounter_doc
CONTAINS OBSERVATION bp[openEHR-EHR-OBSERVATION.blood_pressure.v2]
CONTAINS ELEMENT systolic[at0004]
WHERE systolic/value/magnitude >= 140

Aggregation

Aggregation is for counts, totals, averages, minimums, and maximums. AMCDR supports COUNT, SUM, AVG, MIN, and MAX, including DISTINCT where meaningful.

COUNT All Rows

SELECT COUNT(*) AS composition_count
FROM EHR e
CONTAINS COMPOSITION c

COUNT A Path

SELECT COUNT(c/feeder_audit/originating_system_item_ids/id) AS source_document_count
FROM EHR e
CONTAINS COMPOSITION c
WHERE EXISTS c/feeder_audit/originating_system_item_ids/id

COUNT DISTINCT Patients

SELECT COUNT(DISTINCT e/ehr_status/subject/external_ref/id/value) AS patient_count
FROM EHR e
CONTAINS COMPOSITION c[openEHR-EHR-COMPOSITION.report.v1]
WHERE c/archetype_details/template_id/value = 'Echokardiographie'

SUM, AVG, MIN, MAX

SELECT
  AVG(weight/items[at0004]/value/magnitude) AS average_birth_weight_g,
  MIN(weight/items[at0004]/value/magnitude) AS minimum_birth_weight_g,
  MAX(weight/items[at0004]/value/magnitude) AS maximum_birth_weight_g,
  COUNT(weight/items[at0004]/value/magnitude) AS measured_newborns
FROM EHR e
CONTAINS COMPOSITION birth_record[openEHR-EHR-COMPOSITION.encounter.v1]
CONTAINS CLUSTER weight[openEHR-EHR-CLUSTER.body_weight.v2]
WHERE birth_record/archetype_details/template_id/value = 'neonatal_birth_record.v1'

GROUP BY And HAVING

WHERE filters individual rows before aggregation. HAVING filters aggregate groups after GROUP BY. Do not put aggregate expressions such as COUNT(...) in WHERE.

Count By Template

SELECT
  c/archetype_details/template_id/value AS template_id,
  COUNT(*) AS composition_count
FROM EHR e
CONTAINS COMPOSITION c
GROUP BY c/archetype_details/template_id/value
ORDER BY composition_count DESC

Find Duplicate Order Numbers

SELECT
  c/context/other_context[at0001,'Tree']/items[openEHR-EHR-CLUSTER.case_identification.v0]/items[at0001]/value/value AS encounter_id,
  lab/protocol[at0004]/items[at0094]/items[at0063,'Identifikation der Laboranforderung']/value/id AS order_number,
  COUNT(lab/protocol[at0004]/items[at0094]/items[at0063,'Identifikation der Laboranforderung']/value/id) AS order_count
FROM EHR e
CONTAINS COMPOSITION c
CONTAINS OBSERVATION lab[openEHR-EHR-OBSERVATION.laboratory_test_result.v1]
WHERE c/context/other_context[at0001,'Tree']/items[openEHR-EHR-CLUSTER.case_identification.v0]/items[at0001]/value/value = '122244405'
AND EXISTS c/feeder_audit/originating_system_item_ids/id
GROUP BY
  c/context/other_context[at0001,'Tree']/items[openEHR-EHR-CLUSTER.case_identification.v0]/items[at0001]/value/value,
  lab/protocol[at0004]/items[at0094]/items[at0063,'Identifikation der Laboranforderung']/value/id
HAVING COUNT(lab/protocol[at0004]/items[at0094]/items[at0063,'Identifikation der Laboranforderung']/value/id) >= 2

Patients With At Least Two Lab Results

SELECT
  e/ehr_status/subject/external_ref/id/value AS patient_id,
  COUNT(lab/name/value) AS lab_result_count
FROM EHR e
CONTAINS COMPOSITION c
CONTAINS OBSERVATION lab[openEHR-EHR-OBSERVATION.laboratory_test_result.v1]
GROUP BY e/ehr_status/subject/external_ref/id/value
HAVING COUNT(lab/name/value) >= 2
ORDER BY lab_result_count DESC

Ordering, Pagination, And TOP

AMCDR applies an internal default limit of 1000 rows when a query has no explicit row cap. Explicit LIMIT, TOP, or SQL-style FETCH FIRST/NEXT ... ROWS ONLY overrides that default. Use ORDER BY ... DESC when the caller needs the latest records first.

Quoted Values

Use single quotes for AQL string, date, datetime, and template-id values. For partner compatibility, AMCDR also accepts double-quoted value literals in WHERE predicates and normalizes them internally to canonical single-quoted AQL. Double-quoted aliases remain valid in SELECT projections.

WHERE c/archetype_details/template_id/value = 'newborn_assessment.v1'
WHERE c/archetype_details/template_id/value = "newborn_assessment.v1"
WHERE c/context/start_time/value BETWEEN '2026-06-01' AND '2026-06-15'

Most Recent Rows

SELECT
  c/name/value AS composition_name,
  c/context/start_time/value AS recorded_at
FROM EHR e
CONTAINS COMPOSITION c
ORDER BY recorded_at DESC
LIMIT 25 OFFSET 0

FETCH Syntax

SELECT
  c/name/value AS composition_name,
  c/context/start_time/value AS recorded_at
FROM EHR e
CONTAINS COMPOSITION c[kds-laborbericht]
ORDER BY recorded_at DESC
OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY

TOP Syntax

SELECT TOP 10
  c/name/value AS composition_name,
  c/context/start_time/value AS recorded_at
FROM EHR e
CONTAINS COMPOSITION c
ORDER BY recorded_at DESC

Parameters

Use parameters for saved queries and reports so the same approved query runs across different patients, facilities, date ranges, templates, or organisations.

SELECT
  c/name/value AS composition_name,
  c/context/start_time/value AS recorded_at
FROM EHR e
CONTAINS COMPOSITION c
WHERE e/ehr_status/subject/external_ref/id/value = $patient_id
AND c/context/start_time/value BETWEEN $from_date AND $to_date
ORDER BY recorded_at DESC

Set Operators

Set operators combine complete query results. Use them for cohort logic when two result sets have compatible column shapes.

UNION

SELECT DISTINCT e/ehr_status/subject/external_ref/id/value AS patient_id
FROM EHR e CONTAINS COMPOSITION c
WHERE c/archetype_details/template_id/value = 'diabetes_review.v1'
UNION
SELECT DISTINCT e/ehr_status/subject/external_ref/id/value AS patient_id
FROM EHR e CONTAINS COMPOSITION c
WHERE c/archetype_details/template_id/value = 'hypertension_review.v1'

INTERSECT

SELECT DISTINCT e/ehr_status/subject/external_ref/id/value AS patient_id
FROM EHR e CONTAINS COMPOSITION c
WHERE c/archetype_details/template_id/value = 'adverse_reaction_list.v1'
INTERSECT
SELECT DISTINCT e/ehr_status/subject/external_ref/id/value AS patient_id
FROM EHR e CONTAINS COMPOSITION c
WHERE c/archetype_details/template_id/value = 'medication_list.v0'

EXCEPT

SELECT DISTINCT e/ehr_status/subject/external_ref/id/value AS patient_id
FROM EHR e CONTAINS COMPOSITION c
WHERE c/archetype_details/template_id/value = 'birth_notification.v1'
EXCEPT
SELECT DISTINCT e/ehr_status/subject/external_ref/id/value AS patient_id
FROM EHR e CONTAINS COMPOSITION c
WHERE c/archetype_details/template_id/value = 'newborn_assessment.v1'

Operational Examples

Recent Encounters For One Facility

SELECT
  e/ehr_status/subject/external_ref/id/value AS patient_id,
  c/context/other_context[at0001,'Tree']/items[openEHR-EHR-CLUSTER.case_identification.v0]/items[at0001]/value/value AS encounter_id,
  c/name/value AS document_name,
  c/context/start_time/value AS recorded_at
FROM EHR e
CONTAINS COMPOSITION c
WHERE c/context/health_care_facility/external_ref/id/value = $facility_id
ORDER BY recorded_at DESC
LIMIT 100

Birth Workflow Completeness

SELECT DISTINCT
  e/ehr_status/subject/external_ref/id/value AS newborn_patient_id
FROM EHR e
CONTAINS (
  COMPOSITION birth[openEHR-EHR-COMPOSITION.encounter.v1]
  AND COMPOSITION assessment[openEHR-EHR-COMPOSITION.encounter.v1]
)
WHERE birth/archetype_details/template_id/value = 'birth_notification.v1'
AND assessment/archetype_details/template_id/value = 'newborn_assessment.v1'

Death Documentation Waiting Certification

SELECT
  e/ehr_status/subject/external_ref/id/value AS patient_id,
  death_doc/uid/value AS composition_uid,
  death_doc/context/start_time/value AS reported_at
FROM EHR e
CONTAINS COMPOSITION death_doc[openEHR-EHR-COMPOSITION.report.v1]
WHERE death_doc/archetype_details/template_id/value = 'death_notification.v1'
AND NOT EXISTS death_doc/content[openEHR-EHR-EVALUATION.problem_diagnosis.v1]

Protected Record Review Extract

SELECT
  e/ehr_status/subject/external_ref/id/value AS patient_id,
  COUNT(c/uid/value) AS composition_count
FROM EHR e
CONTAINS COMPOSITION c
GROUP BY e/ehr_status/subject/external_ref/id/value
ORDER BY composition_count DESC

Protected rows are filtered or audited according to protected-record configuration. Do not rely on AQL alone for protected-record authorization.

Execution Endpoints

Technical clients execute AQL through the openEHR-compatible query endpoint. Application reports use saved queries so they remain versioned, audited, parameterized, and reusable.

POST /openehr/v1/query/aql?fetch=50&offset=0
Content-Type: application/json

{
  "q": "SELECT c/name/value AS composition_name FROM EHR e CONTAINS COMPOSITION c LIMIT 50"
}
POST /integration/outbound/saved-queries/{key}/execute
Content-Type: application/json

{
  "parameters": {
    "patient_id": "patient-123",
    "from_date": "2026-01-01",
    "to_date": "2026-12-31"
  }
}

Production Query Rules

Common Mistakes

MistakeUse Instead
WHERE COUNT(path) >= 2GROUP BY ... HAVING COUNT(path) >= 2
JOIN between templatesCONTAINS (COMPOSITION a[...] AND COMPOSITION b[...])
Alias names such as g.hUse g, h, lab_result, then slash paths.
Selecting many raw paths without aliasesAdd stable aliases for saved queries and print layouts.
Using AQL to bypass access policyUse AQL through authenticated AMCDR APIs; access policy still applies.