我有一个SPARQL查询,如下所示-
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现在我只想有一个栏目,而不是“sourced”、“mastered”、“delivered”,“name”是“traceability”。该列将显示一个信息路径是主控数据、源数据还是交付数据,因此我希望绑定(“源数据”作为可追溯性)或(“主控数据”作为可追溯性)或(“交付数据”作为可追溯性)
我是SPARQL的新手,我想知道SPARQL中是否有任何' if‘语句可以用作-
if(?informationPath a dd:SourcedData)
BIND("SourcedData" as ?traceability)任何帮助都将不胜感激。
发布于 2014-04-13 02:52:21
使用bind和if
假设您已经获得了以下形式的数据:
@prefix : <https://stackoverflow.com/q/22985157/1281433/> .
:x1 a :A .
:x2 a :B .
:x3 a :C .
:x4 a :D .然后,您可以使用类似以下的查询来获得一些结果:
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" |
------------------------使用values
它使用if和exists构造来检查各种值。现在,在您的示例中,您有特定数量的需要检查的案例,您实际上可以使用values来模拟一种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" |
------------------发布于 2017-04-25 19:38:21
为了扩展Joshua Taylor的答案,您实际上也可以处理默认情况,这要归功于UNDEF关键字:
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
}https://stackoverflow.com/questions/22985157
复制相似问题