Skip to content

Sparql Query Examples

Guoqian Jiang edited this page Aug 26, 2021 · 10 revisions

Retrieve descendants for condition

PREFIX : <http://hl7.org/fhir/fhir.ttl#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fhir: <http://hl7.org/fhir/>

SELECT DISTINCT ?patient1
        WHERE { 

    ?condition a fhir:Condition .
 	?condition	fhir:Condition.subject	?patient .
	?condition	fhir:Condition.code	?concept_id .

  
	?concept_id    fhir:CodeableConcept.coding [ 
            fhir:Coding.code    [ fhir:value    ?code    ];  
            fhir:Coding.system  [ fhir:value    ?system  ]; 
            fhir:Coding.display [ fhir:value    ?display ] 
	    ] .
  
    FILTER ( str(?system) = 'SNOMED' ) . 
    FILTER ( str(?code) = '57054005' ) . 
        ?x fhir:ConceptMap.sourceUri ?concept_id .
        ?x fhir:ConceptMap.targetUri ?desc_concept_id .
        ?x fhir:ConceptMap.group.element.target.equivalence [ fhir:value "subsumes"^^xsd:string ] .

    ?desc_concept_id    fhir:CodeableConcept.coding [ 
            fhir:Coding.code    [ fhir:value    ?code1    ];  
            fhir:Coding.system  [ fhir:value    ?system1  ]; 
            fhir:Coding.display [ fhir:value    ?display1 ] 
	    ] .

      FILTER ( str(?system1) = 'SNOMED' ) . 
    ?condition1 a fhir:Condition .
 	?condition1	fhir:Condition.subject	?patient1 .
	?condition1	fhir:Condition.code	?desc_concept_id .

  
   }

Retrieve descendants for procedure

PREFIX : <http://hl7.org/fhir/fhir.ttl#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fhir: <http://hl7.org/fhir/>

SELECT DISTINCT ?patient1
        WHERE { 

    ?proc a fhir:Procedure .
 	?proc	fhir:Procedure.subject	?patient .
	?proc	fhir:Procedure.code	?concept_id .
	?concept_id    fhir:CodeableConcept.coding [ 
            fhir:Coding.code    [ fhir:value    ?code    ];  
            fhir:Coding.system  [ fhir:value    ?system  ]; 
            fhir:Coding.display [ fhir:value    ?display ] 
	    ] .
  
    FILTER ( str(?system) = 'SNOMED' ) . 
    FILTER ( str(?code) = '386811000' ) . 

        ?x fhir:ConceptMap.sourceUri ?concept_id .
        ?x fhir:ConceptMap.targetUri ?desc_concept_id .
        ?x fhir:ConceptMap.group.element.target.equivalence [ fhir:value "subsumes"^^xsd:string ] .

    ?desc_concept_id    fhir:CodeableConcept.coding [ 
            fhir:Coding.code    [ fhir:value    ?code1    ];  
            fhir:Coding.system  [ fhir:value    ?system1  ]; 
            fhir:Coding.display [ fhir:value    ?display1 ] 
	    ] .
   
    FILTER ( str(?system1) = 'SNOMED' ) . 

    ?proc1 a fhir:Procedure .
 	?proc1	fhir:Procedure.subject	?patient1 .
	?proc1	fhir:Procedure.code	?desc_concept_id .
 

}

Retrieve descendants for MedicationStatement

PREFIX : <http://hl7.org/fhir/fhir.ttl#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fhir: <http://hl7.org/fhir/>

SELECT DISTINCT ?concept_id ?code ?system ?display
        WHERE { 

    ?med a fhir:MedicationStatement .
 	?med	fhir:MedicationStatement.subject	?patient .
    ?med	fhir:MedicationStatement.medicationCodeableConcept	?concept_id . 

  
	?concept_id    fhir:CodeableConcept.coding [ 
            fhir:Coding.code    [ fhir:value    ?code    ];  
            fhir:Coding.system  [ fhir:value    ?system  ]; 
            fhir:Coding.display [ fhir:value    ?display ] 
	    ] .
  
    #FILTER ( str(?system) = 'RxNorm' ) . 
    FILTER ( str(?code) = '11289' ) . 
        ?x fhir:ConceptMap.sourceUri ?concept_id .
        ?x fhir:ConceptMap.targetUri ?desc_concept_id .
        ?x fhir:ConceptMap.group.element.target.equivalence [ fhir:value "subsumes"^^xsd:string ] .

    ?desc_concept_id    fhir:CodeableConcept.coding [ 
            fhir:Coding.code    [ fhir:value    ?code1    ];  
            fhir:Coding.system  [ fhir:value    ?system1  ]; 
            fhir:Coding.display [ fhir:value    ?display1 ] 
	    ] .

      #FILTER ( str(?system1) = 'RxNorm' ) . 
    ?med1 a fhir:MedicationStatement .
 	?med1	fhir:MedicationStatement.subject	?patient1 .
    ?med1	fhir:MedicationStatement.medicationCodeableConcept	?desc_concept_id . 

  
   }

Retrieve descendants for Observation

PREFIX : <http://hl7.org/fhir/fhir.ttl#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fhir: <http://hl7.org/fhir/>

SELECT (count(?patient1) as ?count)
        WHERE { 

    ?obs a fhir:Observation .
 	?obs	fhir:Observation.subject	?patient .
    ?obs	fhir:Observation.code	?concept_id . 

 	?concept_id    fhir:CodeableConcept.coding [ 
            fhir:Coding.code    [ fhir:value    ?code    ];  
            fhir:Coding.system  [ fhir:value    ?system  ]; 
            fhir:Coding.display [ fhir:value    ?display ] 
	    ] .
  FILTER ( str(?code) = '16206004' )
  
          ?x fhir:ConceptMap.sourceUri ?concept_id .
        ?x fhir:ConceptMap.targetUri ?desc_concept_id .
        ?x fhir:ConceptMap.group.element.target.equivalence [ fhir:value "subsumes"^^xsd:string ] .
  
    ?obs a fhir:Observation .
 	?obs	fhir:Observation.subject	?patient1 .
    ?obs	fhir:Observation.code	?desc_concept_id . 

  
} 

Patient

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX fhir: <http://hl7.org/fhir/>

<http://hl7.org/fhir/Patient/392776072> 
    a fhir:Patient ;
    fhir:Resource.id [fhir:value "392776072"^^xsd:string ];
    fhir:Patient.gender [fhir:value "male"^^xsd:string ];
    fhir:Patient.birthDate [fhir:value  "2138-07-17"^^xsd:date ] .       

Patient

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fhir: <http://hl7.org/fhir/>

SELECT * WHERE {
  ?sub a fhir:Patient .
  ?sub fhir:Patient.gender [fhir:value ?gender ] .
  ?sub fhir:Patient.birthDate [fhir:value ?birthDate ] .
} 
LIMIT 10

Condition

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fhir: <http://hl7.org/fhir/>

SELECT DISTINCT ?patient ?code ?system ?display WHERE {

    ?condition a fhir:Condition .
 	?condition	fhir:Condition.subject	?patient .
	?condition	fhir:Condition.code	[fhir:CodeableConcept.coding [ fhir:Coding.code	[ fhir:value	?code  ];  fhir:Coding.system	[ fhir:value	?system  ]; fhir:Coding.display	[ fhir:value	?display  ] ] ].
	FILTER (str(?code) = '129721000119106' || str(?code) = '140031000119103' || str(?code) = '145681000119101' || str(?code) = '14669001' || str(?code) = '200118004' || str(?code) = '236428007' || str(?code) = '236429004' || str(?code) = '236432001' || str(?code) = '307309005' || str(?code) = '36225005' || str(?code) = '423533009' || str(?code) = '429224003' || str(?code) = '429489008' || str(?code) = '430535006' || str(?code) = '722095005' || str(?code) = '722096006' || str(?code) = '722278006'  )
	FILTER (str(?system) = 'SNOMED' )

}

Condition - Count

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fhir: <http://hl7.org/fhir/>

SELECT DISTINCT ?code ?system ?display (count(?patient) as ?count) WHERE {

    ?condition a fhir:Condition .
 	?condition	fhir:Condition.subject	?patient .
	?condition	fhir:Condition.code	[fhir:CodeableConcept.coding [ fhir:Coding.code	[ fhir:value	?code  ];  fhir:Coding.system	[ fhir:value	?system  ]; fhir:Coding.display	[ fhir:value	?display  ] ] ].
	FILTER (str(?code) = '129721000119106' || str(?code) = '140031000119103' || str(?code) = '145681000119101' || str(?code) = '14669001' || str(?code) = '200118004' || str(?code) = '236428007' || str(?code) = '236429004' || str(?code) = '236432001' || str(?code) = '307309005' || str(?code) = '36225005' || str(?code) = '423533009' || str(?code) = '429224003' || str(?code) = '429489008' || str(?code) = '430535006' || str(?code) = '722095005' || str(?code) = '722096006' || str(?code) = '722278006'  )
	FILTER (str(?system) = 'SNOMED' )

} GROUP BY ?code ?system ?display ORDER BY ?count

Condition

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fhir: <http://hl7.org/fhir/>

SELECT DISTINCT ?patient ?code ?system ?display WHERE {

{
    ?condition a fhir:Condition .
 	?condition	fhir:Condition.subject	?patient .
	?condition	fhir:Condition.code	[fhir:CodeableConcept.coding [ fhir:Coding.code	[ fhir:value	?code  ];  fhir:Coding.system	[ fhir:value	?system  ]; fhir:Coding.display	[ fhir:value	?display  ] ] ].
	FILTER (str(?code) = '1532007' || str(?code) = '195655000' || str(?code) = '195656004' || str(?code) = '195657008' || str(?code) = '195658003' || str(?code) = '195659006' || str(?code) = '195660001' || str(?code) = '195662009' || str(?code) = '232399005' || str(?code) = '232400003' || str(?code) = '363746003' || str(?code) = '40766000' || str(?code) = '43878008' || str(?code) = '58031004' || str(?code) = 'J02.0' || str(?code) = 'J02.8' || str(?code) = 'J02.9'  )
	FILTER (str(?system) = 'SNOMED' || str(?system) = 'ICD10CM' )

}
UNION
{
 	?condition a fhir:Condition .
    ?condition	fhir:Condition.subject	?patient .
	?condition	fhir:Condition.code	[fhir:CodeableConcept.coding [ fhir:Coding.code	[ fhir:value	?code  ];  fhir:Coding.system	[ fhir:value	?system  ]; fhir:Coding.display	[ fhir:value	?display  ] ] ].	FILTER (str(?code) = '10629271000119107' || str(?code) = '17741008' || str(?code) = '195666007' || str(?code) = '195668008' || str(?code) = '195669000' || str(?code) = '195670004' || str(?code) = '195671000' || str(?code) = '195672007' || str(?code) = '195673002' || str(?code) = '195676005' || str(?code) = '195677001' || str(?code) = '302911003' || str(?code) = 'J03.00' || str(?code) = 'J03.01' || str(?code) = 'J03.80' || str(?code) = 'J03.81' || str(?code) = 'J03.90' || str(?code) = 'J03.91'  )
	FILTER (str(?system) = 'SNOMED' || str(?system) = 'ICD10CM' )

}
}

Clone this wiki locally