简体   繁体   中英

Using condition in SPARQL query

I have a SPARQL query which is like this-

SELECT ?informationPath ?businessEntitylabel ?path ?sourced ?mastered ?delivered
WHERE {
?businessEntity dd:hasPropertyPath ?informationPath .
?businessEntity rdfs:label ?businessEntitylabel .
?informationPath dd:hasPath ?path .
OPTIONAL {
?informationPath a dd:SourcedData .
BIND("Yes" as ?sourced)
}
OPTIONAL {
?informationPath a dd:MasteredData .
BIND("Yes" as ?mastered)
}
OPTIONAL {
?informationPath a dd:DeliveredData .
BIND("Yes" as ?delivered)
}
} ORDER BY ?businessEntitylabel ?path

Now I want to have only one column instead of ?sourced ?mastered ?delivered and name is ?traceability. And the column will show if an ?informationPath is Mastered data or Sourced data or delivered data and accordingly I want to BIND ("Sourced data" as ?traceability) or ("Mastered data" as ?traceability) or ("Delivered data" as ?traceability)

I am new in SPARQL, and I was wondering if there is any 'if' statement in SPARQL which can be used as-

if(?informationPath a dd:SourcedData)
BIND("SourcedData" as ?traceability)

Any help will be much appreciated.

Using bind and if

I think that this is a duplicate of Binding a variable to one of two values with IF? , and I've marked it as such, but it might not be immediately obvious what the conditions in the if should be, so I'm adding this example as an answer. Suppose you've got data of the form:

@prefix : <https://stackoverflow.com/q/22985157/1281433/> .

:x1 a :A .
:x2 a :B .
:x3 a :C .
:x4 a :D .

Then you can use a query like the following to get some results:

prefix : <https://stackoverflow.com/q/22985157/1281433/>

select ?x ?typename where { 
  ?x a [] .
  bind( if ( exists { ?x a :A },
             "type A" ,
             if ( exists { ?x a :B }, 
                  "type B",
                  if ( exists { ?x a :C },
                       "type C",
                       "unknown type" )))
        as ?typename )
}
------------------------
| x   | typename       |
========================
| :x1 | "type A"       |
| :x2 | "type B"       |
| :x3 | "type C"       |
| :x4 | "unknown type" |
------------------------

Using values

That uses if and the exists construct to check various values. Now, in your case, where you have a specific number of cases that you want to check for, you can actually use values to simulate a sort of case statement. To do that, you'd do something like this, although this won't give you an "unknown" case.

prefix : <https://stackoverflow.com/q/22985157/1281433/>

select ?x ?typename where { 
  values (?type ?typename) { 
    (:A "type A")
    (:B "type B")
    (:C "type C")
  }
  ?x a ?type
}
------------------
| x   | typename |
==================
| :x1 | "type A" |
| :x2 | "type B" |
| :x3 | "type C" |
------------------

To expand on Joshua Taylor's answer, you actually can handle the default case as well thanks to the UNDEF keyword:

prefix : <http://stackoverflow.com/q/22985157/1281433/>

select ?x ?typename where { 
  values (?type ?typename) { 
    (:A "type A")
    (:B "type B")
    (:C "type C")
    (UNDEF "unknown type")
  }
  ?x a ?type
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM