Find coded Physical Quantities in CCDs

Synopsis

This query retrieves coded physical quantity (PQ) values and unit, and if present the interpretation code. PQ values are typically found in Lab Results and Vital Signs sections of CCD documents.

Processing Requirements

For this query, Continuity of Care Documents were processed. See processing requirements Processing Requirements.

Clinical Context

CCDs often contain coded measurement observations coded using LOINC and their results. The results of these measurements are dimensioned quantities that are encoded using the PQ data type (in FHIR RIM v3 data model). The PQ data type includes the measurement value and its unit. The interpretation of the PQ values is sometimes also provide as shown in the following image.

Lab result coded entry

SQL query

WITH coded_data AS (
SELECT DISTINCT d.id as docid, regexp_replace(d.filename, '.*/', '') AS filename,
dm.value::jsonb ->> 'class_code' as class_code,
dm.value::jsonb ->> 'text' as sentence,
dm.value::jsonb as json
FROM document d
JOIN documentstructuredmetadata dm ON d.id = dm.document_id
),
testresults AS (
SELECT res_cd.docid,res_cd.filename as filename,
sec_cd.json ->> 'display_name' as section,
res_cd.json ->> 'display_name' as display_name,
res_cd.json ->> 'text_id' as text_id,
res_cd.json ->> 'label' as label,
res_cd.json ->> 'code_system_name' as code_system_name,
res_cd.json ->> 'code_system' as code_system,
res_cd.json ->> 'code' as code,
res_cd.json #>> '{value,value}' as value,
res_cd.json #>> '{value,unit}' as unit,
res_cd.json #>> '{value,type}' as value_type,
res_cd.sentence,
res_cd.json
FROM coded_data res_cd
LEFT JOIN coded_data sec_cd ON
--> Get section display_name from section entries
res_cd.json ->> 'section' = sec_cd.json ->> 'label' AND
sec_cd.json ->> 'for_elt_tag' = 'section'
WHERE res_cd.json #>> '{value,type}'='PQ' AND res_cd.json ->> 'code_system_name' = 'LOINC'),
interpretations AS (
--> Get interpretationCode display_name from interpretation entries
SELECT json ->> 'display_name' as display_name,
json ->> 'text_id' as text_id,
json ->> 'parent_reference' as parent_reference,
json ->> 'code_system_name' as code_system_name,
json ->> 'code_system' as code_system,
json ->> 'code' as code,
json
FROM coded_data
WHERE json ->> 'code_elt_tag' = 'interpretationCode'
)
SELECT tr.docid, tr.filename, tr.section AS section_display_name, tr.text_id,
tr.display_name, tr.code_system_name, tr.code_system, tr.code,
tr.value,tr.unit, tr.value_type,
interp.code AS interpret_code,
interp.code_system AS interpret_code_system,
tr.sentence
FROM testresults tr
LEFT JOIN interpretations interp ON tr.label = interp.parent_reference
ORDER BY tr.docid, tr.section, tr.text_id;
docidfilenamesection_display_nametext_iddisplay_namecode_system_namecode_systemcodevalueunitvalue_typeinterpret_codeinterpret_code_systemsentence
138emerge-patient-1.xmlResultsHDLc SerPl-sCncLOINC2.16.840.1.113883.6.114646-461mg/dLPQN2.16.840.1.113883.5.83
138emerge-patient-1.xmlResultsLDLc SerPl-mCncLOINC2.16.840.1.113883.6.12089-1106.6mg/dLPQN2.16.840.1.113883.5.83
138emerge-patient-1.xmlResultsTrigl SerPl Calc-mCncLOINC2.16.840.1.113883.6.112951-0189mg/dLPQN2.16.840.1.113883.5.83
138emerge-patient-1.xmlResultsHgb A1c MFr BldLOINC2.16.840.1.113883.6.14548-46.7%PQN2.16.840.1.113883.5.83
138emerge-patient-1.xmlResultsHDLc SerPl-sCncLOINC2.16.840.1.113883.6.114646-463mg/dLPQN2.16.840.1.113883.5.83
138emerge-patient-1.xmlResultsCholest SerPl-mCncLOINC2.16.840.1.113883.6.12093-3251mg/dLPQN2.16.840.1.113883.5.83