FHIR Chat · Aggregate Queries · javascript

Stream: javascript

Topic: Aggregate Queries


view this post on Zulip Rebecca Green (Oct 27 2021 at 17:54):

Does the cql-execution library support the new Aggregate Queries functionality? https://cql.hl7.org/2020May/03-developersguide.html#aggregate-queries

view this post on Zulip Chris Moesel (Oct 27 2021 at 19:37):

Yes. Support for aggregate queries was added to cql-execution in version 2.3.0.

view this post on Zulip Rebecca Green (Oct 27 2021 at 19:50):

Thanks! Just found the answer in the release notes before you posted your reply. I'm confused about how to set the initial aggregate value in cql. If I want the initial value to be 0, how would I express that in CQL?

view this post on Zulip Bryn Rhodes (Oct 28 2021 at 02:13):

define Sum: ({ 1, 2, 3, 4, 5 }) X aggregate Result starting 0: Result + Result

view this post on Zulip Bryn Rhodes (Oct 28 2021 at 02:14):

Without the starting clause, the Result accumulator is initially null

view this post on Zulip Rebecca Green (Oct 29 2021 at 13:23):

Does an aggregate have to be an Integer or can it be something else like a Tuple for example?

view this post on Zulip Rebecca Green (Oct 29 2021 at 13:23):

If so how would you express that?

view this post on Zulip Rebecca Green (Oct 29 2021 at 15:38):

For example I'm trying to write a function similar to this:

define function AggregateFunction():
  (expand Interval[1, 4]) C
  aggregate S starting ({
    A: null,
    B: null,
    C: null,
    D: null
  } as Tuple {
    A Boolean,
    B Boolean,
    C Boolean,
    D Boolean
  }):
  case
    when C > 1 then S.A is true
    when C = 1 then S.B is true
    when C < 1 then S.C is true
    when C = 0 then S.D is true
    else S
  end

But I'm getting an error this error: Expected an expression of type 'System.Boolean', but found an expression of type 'tuple of A: System.Boolean, B: System.Boolean, C: System.Boolean, D: System.Boolean'.

I thought that by specifying the starting value as the tuple the cql would expect the tuple as a response. Any help? Thoughts on what I'm getting wrong here?

view this post on Zulip Bryn Rhodes (Nov 02 2021 at 22:04):

The aggregate can be any type, the accumulator expression is responsible for determining what "accumulating" means for each iteration. This example from the spec, for instance, accumulates sets of intervals:

define "RolledOutIntervals":
  MedicationRequestIntervals M
    aggregate R starting (null as List<Interval<DateTime>>): R union ({
      M X
        let S: Max({ end of Last(R) + 1 day, start of X }),
          E: S + duration in days of X
        return Interval[S, E]
    })

Last updated: Apr 12 2022 at 19:14 UTC