Skip to content
Alpha: see what works today →
Menu

Named question about pod

My active medications

Read from the view alone, as an app would: each entry whose status is of the active lifecycle class (rule L1), so not one on hold, stopped, not taken or without a status, with its RxNorm code, when it started, the day its prescription was written, how many records it joins, each record's own status and the day it ended, the dosage text each record states with the day its prescription was written where it states one, in order of that day and the records stating none last (none when no record states one; a record that states a written day and no dosage text is not in it), the text each record's source wrote for it, such as a note beside the row (none when no record has one), what each record was given for: each condition it names, by the condition's entry and name, and each code of its reason, as "<record> for <entry>, <name>", "<record> for <code IRI>" and, for a code in a system the vocabulary lists none of, "<record> for <system IRI> <code>" (none when no record gives a reason, and no condition whose record is in no entry). Each item puts its IRIs first and its one free-text part last, so a reader takes its kind from the token after "for " up to the first space, never from the text: a pod IRI then "," is a condition entry (then its name, which may hold anything); otherwise a pod IRI alone is a condition entry with no name, an IRI that is not the pod's alone is a code in a listed system, and an IRI followed by a space and more text is a system IRI and its code (as written, which may hold spaces and commas). Items are in code point order of the name or code (a condition with no name first), then the entry or system, then the record, and each record with the document it was derived from, when that was retrieved, and the hospitals that wrote it and that keep it. An entry whose status was taken from a record that stopped it is not listed, even when another record says it is active: that record's status is in `pod/My medication history`. The code is RxNorm's IRI, or https://ns.cascadeprotocol.org/codes/rxnorm/ followed by the code as written, percent-encoded as ENCODE_FOR_URI does (rule M4c). It is the chosen RxNorm code, as the rule that joins medications by a shared code reads it: the code the source selected, when that is an RxNorm code; else the entry's one RxNorm code, even when the source selected an NDC; none when it states several and the source chose none. A pod holds no table, so it cannot tell a product from an ingredient among several, and no code stands in for the choice. A list holds its values in order, separated by U+001F, which no value holds; a value's own parts are ended by ", ".

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 ?medication ?code ?started ?written ?statuses ?dosages ?narrativeTexts ?reasons (COUNT(?record) AS ?records) ?entry
       ?entryLabel ?sources
WHERE {
  { SELECT ?entry (REPLACE(GROUP_CONCAT(DISTINCT CONCAT(?statusText, "\u001F"); separator=""), "\u001F$", "") AS ?statuses)
    WHERE {
      { SELECT ?entry ?statusText
        WHERE {
          ?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ;
                 cascade:memberCurrentValues ?values .
          ?values cascade:valuesOfMember ?member .
          OPTIONAL { ?values cascade:workflowStatus ?memberStatus }
          OPTIONAL { ?values cascade:medicationUseEndDate ?memberEnded }
          BIND (CONCAT(STR(?member), " ", COALESCE(?memberStatus, "with no status"),
                       COALESCE(CONCAT(", ended ", STR(?memberEnded)), "")) AS ?statusText)
        }
        ORDER BY ?entry ?statusText }
    }
    GROUP BY ?entry }
  { SELECT ?entry (REPLACE(GROUP_CONCAT(DISTINCT ?dosageText; separator=""), "\u001F$", "") AS ?stated)
    WHERE {
      { SELECT ?entry ?dosageText
        WHERE {
          ?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ;
                 cascade:memberCurrentValues ?values .
          ?values cascade:valuesOfMember ?member .
          OPTIONAL { ?values cascade:dosageInstructionText ?memberDosage }
          OPTIONAL { ?values cascade:prescriptionWrittenDate ?memberWritten }
          BIND (IF(BOUND(?memberDosage),
                   CONCAT(STR(?member), " ", ?memberDosage, COALESCE(CONCAT(", written ", STR(?memberWritten)), ""), "\u001F"), "") AS ?dosageText)
          BIND (COALESCE(STR(?memberWritten), "~") AS ?writtenKey)
        }
        ORDER BY ?entry ?writtenKey ?dosageText }
    }
    GROUP BY ?entry }
  { SELECT ?entry (REPLACE(GROUP_CONCAT(DISTINCT ?narrativeLine; separator=""), "\u001F$", "") AS ?said)
    WHERE {
      { SELECT ?entry ?narrativeLine
        WHERE {
          ?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ;
                 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 ?reasonLine; separator=""), "\u001F$", "") AS ?given)
    WHERE {
      { SELECT ?entry ?reasonLine
        WHERE {
          { ?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive .
            BIND ("" AS ?reasonLine) }
          UNION
          { ?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ;
                   cascade:memberCurrentValues ?values .
            ?values cascade:valuesOfMember ?member ; cascade:reasonCondition ?conditionRecord .
            ?conditionEntry cascade:entryMember ?conditionRecord .
            OPTIONAL { ?conditionEntry cascade:conditionName ?conditionName }
            BIND (CONCAT(STR(?member), " for ", STR(?conditionEntry), COALESCE(CONCAT(", ", ?conditionName), ""), "\u001F") AS ?reasonLine)
            BIND (COALESCE(?conditionName, "") AS ?reasonKey)
            BIND (STR(?conditionEntry) AS ?reasonTail) }
          UNION
          { ?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ;
                   cascade:memberCurrentValues ?values .
            ?values cascade:valuesOfMember ?member ; cascade:reasonCode ?reasonCode .
            BIND (CONCAT(STR(?member), " for ", STR(?reasonCode), "\u001F") AS ?reasonLine)
            BIND (STR(?reasonCode) AS ?reasonKey)
            BIND ("" AS ?reasonTail) }
          UNION
          { ?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ;
                   cascade:memberCurrentValues ?values .
            ?values cascade:valuesOfMember ?member ; cascade:reasonCodeInOtherSystem ?otherCode .
            BIND (CONCAT(STR(?member), " for ", STR(DATATYPE(?otherCode)), " ", STR(?otherCode), "\u001F") AS ?reasonLine)
            BIND (STR(?otherCode) AS ?reasonKey)
            BIND (STR(DATATYPE(?otherCode)) AS ?reasonTail) }
        }
        ORDER BY ?entry ?reasonKey ?reasonTail ?member }
    }
    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:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ;
                         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 }
  {
    SELECT ?entry (COUNT(DISTINCT ?candidate) AS ?candidates) (SAMPLE(?candidate) AS ?only)
    WHERE {
      ?entry a cascade:PrescribedOrReportedMedication .
      OPTIONAL {
        ?entry cascade:rxNormCode ?written .
        BIND (IF(isIRI(?written), ?written, IRI(CONCAT("https://ns.cascadeprotocol.org/codes/rxnorm/", ENCODE_FOR_URI(STR(?written))))) AS ?candidate)
      }
    }
    GROUP BY ?entry
  }
  ?entry a cascade:PrescribedOrReportedMedication ; cascade:lifecycle cascade:MedicationActive ;
         cascade:drugName ?medication ; cascade:entryMember ?record .
  OPTIONAL { ?entry cascade:userSelectedCode ?chosen }
  OPTIONAL { ?entry cascade:medicationUseStartDate ?started }
  OPTIONAL { ?entry cascade:prescriptionWrittenDate ?written }
  OPTIONAL { ?entry rdfs:label ?entryLabel }
  BIND (IF(?stated = "", ?nothing, ?stated) AS ?dosages)
  BIND (IF(?said = "", ?nothing, ?said) AS ?narrativeTexts)
  BIND (IF(?given = "", ?nothing, ?given) AS ?reasons)
  BIND (COALESCE(IF(STRSTARTS(STR(?chosen), "https://ns.cascadeprotocol.org/codes/rxnorm/"), ?chosen, ?nothing),
                 IF(?candidates = 1, ?only, ?nothing)) AS ?code)
}
GROUP BY ?entry ?medication ?code ?started ?written ?entryLabel ?statuses ?dosages ?narrativeTexts ?reasons ?sources
ORDER BY ?medication ?entry

Terms it reads

cascade:AuthorRole · cascade:CustodianRole · cascade:MedicationActive · cascade:PrescribedOrReportedMedication · cascade:conditionName · cascade:currentRevision · cascade:dosageInstructionText · cascade:drugName · cascade:entryMember · cascade:lifecycle · cascade:medicationUseEndDate · cascade:medicationUseStartDate · cascade:memberCurrentValues · cascade:narrativeText · cascade:prescriptionWrittenDate · cascade:reasonCode · cascade:reasonCodeInOtherSystem · cascade:reasonCondition · cascade:rxNormCode · cascade:userSelectedCode · cascade:valuesOfMember · cascade:workflowStatus · 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 active medications.rq on GitHub ↗