0

I have this sparql query that I need to modify. I have the example instance, which has the exampleID 12345. For this case, if the :hasRelatedExample connection exists, the ?test variable will become 2. If the :hasRelatedExample connection doesn't exist from the example instance, the ?test variable doesn't get assigned the 1 value as it should. How could I fix this query to reflect the needed behavior?

PREFIX : <http://www.example.com#>
Select distinct  ?test 
  where
  {
?ex a :Example ;
     :exampleID "12345" ;
     :hasRelatedExample ?relatedExample .

BIND (IF(BOUND(?relatedExample),2,1) as ?test)
}   
moro_92
  • 171
  • 1
  • 2
  • 10
  • you have to put the triple pattern into an `OPTIONAL`, i.e. `?ex :hasRelatedExample ?relatedExample .` to allow for absence of the data – UninformedUser Apr 06 '22 at 12:31
  • 1
    alternatively, use `exists` operator and omit the triple pattern: `PREFIX : Select distinct ?test where { ?ex a :Example ; :exampleID "12345" . BIND (IF(EXISTS {?ex :hasRelatedExample ?relatedExample}, 2, 1) as ?test) } ` – UninformedUser Apr 06 '22 at 12:33

0 Answers0