Skip to content
Alpha: see what works today →
Menu

Matcher rule

same-product-code-in-current-tables-and-overlapping-exposure-periods

Two records of one kind match when their current versions share an RxNorm code that a version of a drug products table lists, so the code names one drug product (SCD, SBD, GPCK or BPCK), and their exposure periods share at least one day, so two courses of one product years apart stay two medications (decisions/medication-matching.md). A period runs from cascade:medicationExposureStart to cascade:medicationExposureEnd, both days included; with no end it runs on when the record's exposure is ongoing, and is its start day alone otherwise. A record with no exposure start matches nothing. Codings are equals: any shared product code joins, and cascade:userSelectedCode plays no part. A retired product code still joins, since RxNorm never reuses a code. Left alone: a shared ingredient, strength, dose form, brand name or salt (IN, MIN, PIN, BN, SCDC, SCDF, SCDG and their branded forms); a code no current version knows, which is every RxNorm code when no drug products table is held; and a pair that shares an NDC, which the rule that joins by NDC has, so no pair is judged by both. A version that carries more than one code that a current drug products version lists is left out, whatever their term types, because it would join two products and entries would then join through it: A with X, B with X and Y, C with Y would make A, B and C one entry though A and C share nothing. That includes a pack and a component, a pen and a vial, and a brand and its generic, which are left for a person to confirm. The codes are counted over the current version of every drug products series, the one no version revises, so that a series listing fewer products cannot make a record look single-product; an older version's codes are never counted. A known limit: a product code that no current version lists, such as one newer than the table, is not counted, so a record that carries it beside a listed component joins on the component; it goes to review instead once a current version lists it. Each ordered pair is a row, with the version whose rows joined it; a graph that is not a version of a drug products table is never read for rows. A run binds ?record to each record it compares.

The query

PREFIX cascade: <https://ns.cascadeprotocol.org/cascade#>
PREFIX pav: <http://purl.org/pav/>
PREFIX prov: <http://www.w3.org/ns/prov#>

SELECT DISTINCT ?record ?other ?origin
WHERE {
  { SELECT ?record ?version ?kind (COALESCE(SAMPLE(?held), "") AS ?code)
    WHERE {
      ?current prov:specializationOf/cascade:tableKind cascade:DrugProducts .
      FILTER NOT EXISTS { ?newer prov:wasRevisionOf ?current }
      ?version cascade:rxNormCode ?held .
      ?record pav:hasCurrentVersion ?version ; cascade:recordKind ?kind .
      GRAPH ?current { ?held cascade:termType ?type }
    }
    GROUP BY ?record ?version ?kind
    HAVING (COUNT(DISTINCT ?held) = 1) }
  { SELECT ?other ?otherVersion ?kind (COALESCE(SAMPLE(?otherHeld), "") AS ?code)
    WHERE {
      ?current prov:specializationOf/cascade:tableKind cascade:DrugProducts .
      FILTER NOT EXISTS { ?newer prov:wasRevisionOf ?current }
      ?otherVersion cascade:rxNormCode ?otherHeld .
      ?other pav:hasCurrentVersion ?otherVersion ; cascade:recordKind ?kind .
      GRAPH ?current { ?otherHeld cascade:termType ?otherType }
    }
    GROUP BY ?other ?otherVersion ?kind
    HAVING (COUNT(DISTINCT ?otherHeld) = 1) }
  ?origin prov:specializationOf/cascade:tableKind cascade:DrugProducts .
  GRAPH ?origin { ?code cascade:termType ?codeType }
  ?record pav:hasCurrentVersion ?version .
  FILTER (?record != ?other)
  FILTER NOT EXISTS {
    ?version cascade:ndcCode ?shared .
    ?otherVersion cascade:ndcCode ?shared
  }
  ?record cascade:medicationExposureStart ?start .
  ?other cascade:medicationExposureStart ?otherStart .
  OPTIONAL { ?record cascade:medicationExposureEnd ?end }
  OPTIONAL { ?record cascade:medicationExposureOngoing ?ongoing }
  OPTIONAL { ?other cascade:medicationExposureEnd ?otherEnd }
  OPTIONAL { ?other cascade:medicationExposureOngoing ?otherOngoing }
  FILTER (STR(?start) <= COALESCE(STR(?otherEnd), IF(BOUND(?otherOngoing), "9999-12-31", STR(?otherStart)))
          && STR(?otherStart) <= COALESCE(STR(?end), IF(BOUND(?ongoing), "9999-12-31", STR(?start))))
}

Terms it reads

cascade:DrugProducts · cascade:medicationExposureEnd · cascade:medicationExposureOngoing · cascade:medicationExposureStart · cascade:ndcCode · cascade:recordKind · cascade:rxNormCode · cascade:termType · pav:hasCurrentVersion · prov:specializationOf · prov:wasRevisionOf

Rules and decision notes that name it

queries/matcher/same-product-code-in-current-tables-and-overlapping-exposure-periods.rq on GitHub ↗