Skip to content
Alpha: see what works today →
Menu

Named question about pod

My lab results

Read from the view alone, as an app would, newest first, with the interpretation flags its source gave each entry, each the code of HL7's ObservationInterpretation, its reference range as text and each bound as written, with its unit, the text each record's source wrote for it, such as a label beside the value (none when no record has one), how many records each entry joins, and each record it joins, with the document it was derived from, when that was retrieved, and the hospitals that wrote it and that keep it. A list holds its values in order, separated by U+001F, and a list with nothing in it is unbound; a value's own parts are ended by ", ". A day and a time are not comparable, so entries are ordered by their day first, then by the time as written, and an entry's rows by code.

The query

PREFIX cascade: <https://ns.cascadeprotocol.org/cascade#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX pav: <http://purl.org/pav/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?performed ?test ?code ?value ?unit ?status ?interpretations ?referenceRangeText ?referenceRangeLow ?referenceRangeHigh
       ?narrativeTexts (COUNT(?record) AS ?records) ?entry ?entryLabel ?sources
WHERE {
  { SELECT ?entry (REPLACE(GROUP_CONCAT(DISTINCT ?narrativeLine; separator=""), "\u001F$", "") AS ?said)
    WHERE {
      { SELECT ?entry ?narrativeLine
        WHERE {
          ?entry a cascade:LaboratoryTestResult ; cascade:memberCurrentValues ?values .
          ?values cascade:valuesOfMember ?member .
          OPTIONAL { ?values cascade:narrativeText ?memberNarrativeText }
          BIND (IF(BOUND(?memberNarrativeText), CONCAT(STR(?member), " ", ?memberNarrativeText, "\u001F"), "") AS ?narrativeLine)
        }
        ORDER BY ?entry ?narrativeLine }
    }
    GROUP BY ?entry }
  { SELECT ?entry (REPLACE(GROUP_CONCAT(DISTINCT COALESCE(CONCAT(?flag, "\u001F"), ""); separator=""), "\u001F$", "") AS ?flags)
    WHERE {
      { SELECT ?entry ?flag
        WHERE {
          ?entry a cascade:LaboratoryTestResult .
          OPTIONAL { ?entry cascade:resultInterpretationCode ?interpretation }
          BIND (REPLACE(STR(?interpretation), "^.*/", "") AS ?flag)
        }
        ORDER BY ?entry ?flag }
    }
    GROUP BY ?entry }
  { SELECT ?entry (REPLACE(GROUP_CONCAT(DISTINCT CONCAT(?sourceText, "\u001F"); separator=""), "\u001F$", "") AS ?sources)
    WHERE {
      { SELECT ?entry ?sourceText
        WHERE {
          { SELECT ?entry ?sourceRecord ?document ?retrievedOn
                   (REPLACE(GROUP_CONCAT(DISTINCT COALESCE(CONCAT(?hospital, ", "), ""); separator=""), ", $", "") AS ?hospitals)
            WHERE {
              { SELECT ?entry ?sourceRecord ?document ?retrievedOn ?hospital
                WHERE {
                  ?entry a cascade:LaboratoryTestResult ; cascade:entryMember ?sourceRecord .
                  OPTIONAL { ?sourceRecord cascade:currentRevision/prov:wasDerivedFrom ?document .
                             OPTIONAL { ?document pav:retrievedOn ?retrievedOn }
                             OPTIONAL { ?document prov:wasGeneratedBy/prov:qualifiedAssociation ?association .
                                        ?association prov:hadRole ?hospitalRole ; prov:agent ?agent
                                        FILTER (?hospitalRole IN (cascade:AuthorRole, cascade:CustodianRole))
                                        OPTIONAL { ?agent rdfs:label ?agentLabel }
                                        OPTIONAL { ?agent pav:hasCurrentVersion/foaf:name ?agentName }
                                        BIND (CONCAT(IF(?hospitalRole = cascade:AuthorRole, "written by ", "kept by "),
                                                     COALESCE(?agentLabel, ?agentName)) AS ?hospital) } }
                }
                ORDER BY ?entry ?sourceRecord ?document ?hospital }
            }
            GROUP BY ?entry ?sourceRecord ?document ?retrievedOn }
          BIND (CONCAT(STR(?sourceRecord), COALESCE(CONCAT(" from ", STR(?document)), ""),
                       COALESCE(CONCAT(", retrieved ", STR(?retrievedOn)), ""),
                       IF(?hospitals = "", "", CONCAT(", ", ?hospitals))) AS ?sourceText)
        }
        ORDER BY ?entry ?sourceText }
    }
    GROUP BY ?entry }
  ?entry a cascade:LaboratoryTestResult ; cascade:entryMember ?record .
  OPTIONAL { ?entry cascade:resultClinicallyRelevantTime ?performed }
  OPTIONAL { ?entry cascade:testName ?test }
  OPTIONAL { ?entry cascade:testCode ?code }
  OPTIONAL { ?entry cascade:resultValueText ?value }
  OPTIONAL { ?entry cascade:resultUnitText ?unit }
  OPTIONAL { ?entry cascade:workflowStatus ?status }
  OPTIONAL { ?entry cascade:referenceRangeText ?referenceRangeText }
  OPTIONAL { ?entry cascade:referenceRangeLow ?low .
             ?low cascade:writtenValue ?lowWritten .
             OPTIONAL { ?low cascade:unitText ?lowUnitText }
             OPTIONAL { ?low cascade:ucumUnit ?lowUcum } }
  OPTIONAL { ?entry cascade:referenceRangeHigh ?high .
             ?high cascade:writtenValue ?highWritten .
             OPTIONAL { ?high cascade:unitText ?highUnitText }
             OPTIONAL { ?high cascade:ucumUnit ?highUcum } }
  OPTIONAL { ?entry rdfs:label ?entryLabel }
  BIND (IF(?flags = "", 1/0, ?flags) AS ?interpretations)
  BIND (IF(?said = "", 1/0, ?said) AS ?narrativeTexts)
  BIND (CONCAT(?lowWritten, COALESCE(CONCAT(" ", COALESCE(?lowUnitText, ?lowUcum)), "")) AS ?referenceRangeLow)
  BIND (CONCAT(?highWritten, COALESCE(CONCAT(" ", COALESCE(?highUnitText, ?highUcum)), "")) AS ?referenceRangeHigh)
}
GROUP BY ?entry ?performed ?test ?code ?value ?unit ?status ?interpretations ?referenceRangeText ?referenceRangeLow
         ?referenceRangeHigh ?narrativeTexts ?entryLabel ?sources
ORDER BY DESC(COALESCE(SUBSTR(STR(?performed), 1, 10), "")) DESC(COALESCE(STR(?performed), "")) ?test ?entry ?code

Terms it reads

cascade:AuthorRole · cascade:CustodianRole · cascade:LaboratoryTestResult · cascade:currentRevision · cascade:entryMember · cascade:memberCurrentValues · cascade:narrativeText · cascade:referenceRangeHigh · cascade:referenceRangeLow · cascade:referenceRangeText · cascade:resultClinicallyRelevantTime · cascade:resultInterpretationCode · cascade:resultUnitText · cascade:resultValueText · cascade:testCode · cascade:testName · cascade:ucumUnit · cascade:unitText · cascade:valuesOfMember · cascade:workflowStatus · cascade:writtenValue · pav:hasCurrentVersion · pav:retrievedOn · prov:agent · prov:hadRole · prov:wasGeneratedBy · rdfs:label

Rules and decision notes that name it

No rule or decision note writes its path.

Its rows on Alex Rivera's pod

Alex Rivera's pod gives this question no rows. Open it in Alex Rivera's explorer.

queries/questions/pod/My lab results.rq on GitHub ↗