QA Validation Error Due to Aliases in Sort By - cqframework/CQL-Formatting-and-Usage-Wiki GitHub Wiki

Questions and Answers


Q&A - Validation Error Due to Aliases in Sort-By

Question

The following definition is returning a validation error which was not showing up prior to the most recent MAT staging update.

  define "Most Recent Adult Depression Screening":
    Last (
      ["Assessment, Performed": "Adult Depression Screening"] Adultscreen 
        with "Depression Screening Encounter" Enc
          such that Adultscreen.authorDatetime during Enc.relevantPeriod and Adultscreen.result is not null
        sort by Adultscreen.authorDatetime
    )

If I remove "sort by Adultscreen.authorDatetime" the definition saves without error:

  define "Depression Screening Encounter":
    ["Encounter, Performed": "Depression Screening Encounter Codes"] Enc
      where Enc.relevantPeriod during "Measurement Period"

Answer

Your sort clause is incorrect because it is evaluated on the result of the query. Aliases are used in the calculation of the results. Consider a query with a return clause, or a multi-source query, the aliases no longer have meaning once the results are produced.