Skip to content
Alpha: see what works today →
Menu

Named question about pod

My medication history

Read from the view alone, as an app would: each medication entry whatever its status, with its RxNorm code, its status, the lifecycle class of that status (rule L1), when it started, the day its prescription was written, when it ended, each record's own status and the day it ended, so a stop one record states is shown beside another's "active" whichever the entry took, 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, so a dose changed reads in the order it was written (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 stop reason (none when no record has one), what each record was given for, as `pod/My active medications` gives it: "<record> for <entry>, <name>" for a condition (an entry with no name gives "<record> for <entry>"), "<record> for <code IRI>" for a code in a listed system and "<record> for <system IRI> <code>" for a code in another system, each read from the token after "for " up to the first space, never from the text: a pod IRI then "," is a condition entry; 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, in code point order of the name or code, then the entry or system, then the record (none when no record gives a reason), how many records it joins, and each record with the document it was derived from, when that was retrieved, and the hospitals that wrote it and that keep it. A source that states no stop gives no end date: none is inferred from a medication's last appearance. The code is the one `pod/My active medications` gives, and the entries whose lifecycle is cascade:MedicationActive are the ones it lists. 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 ?status ?lifecycle ?started ?written ?ended ?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: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: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: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 .
            BIND ("" AS ?reasonLine) }
          UNION
          { ?entry a cascade:PrescribedOrReportedMedication ; 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: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: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: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:drugName ?medication ; cascade:entryMember ?record .
  OPTIONAL { ?entry cascade:userSelectedCode ?chosen }
  OPTIONAL { ?entry cascade:workflowStatus ?status }
  OPTIONAL { ?entry cascade:lifecycle ?lifecycle }
  OPTIONAL { ?entry cascade:medicationUseStartDate ?started }
  OPTIONAL { ?entry cascade:prescriptionWrittenDate ?written }
  OPTIONAL { ?entry cascade:medicationUseEndDate ?ended }
  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 ?status ?lifecycle ?started ?written ?ended ?statuses ?dosages ?narrativeTexts ?reasons ?entryLabel ?sources
ORDER BY ?medication ?entry

Terms it reads

cascade:AuthorRole · cascade:CustodianRole · 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 medication history.rq on GitHub ↗