排除具有相同 属性 和值的结果
Exclude results which have the same property and value
如果我有这个数据:
:a :p :x; a :C .
:b :p :y; a :C .
:c :p :y; a :C .
:d :q :z; a :C .
:e :p :y; a :C .
如何对 select 第一个 ?s :p :y
进行 SPARQL 查询并排除任何其他。
换句话说,如何更改此查询:
SELECT *
WHERE {?s a :C}
所以在这个数据上,应该只有两个结果,:d
和 :b
或 :c
或 :e
。这无关紧要,因为它取决于与模式匹配的顺序,我想这不在查询的控制范围内。
注意:我正在简化实际情况,其中有一组模式,而不仅仅是 ?s a :C,但想法是,如果一些匹配的三元组也与 :p
链接,则应该是 ?s :p ?o
模式中相同 ?o
的一个结果。
您可以使用 LIMIT
将结果限制为只有一行:
SELECT ?s {
?s :p :y .
} LIMIT 1
您可以使用 ORDER BY
来影响结果的顺序,从而影响将要选择的资源。
您可以按 ?p 和 ?o 分组,然后从每组中的 ?s 值中抽样:
select (sample(?s_) as ?s) ?p ?o where {
?s_ a :C .
?s_ ?p ?o.
#-- Filter here is used to exclude the property that was used
#-- for selecting individuals (?s_ a :C). In general, this
#-- just needs to make sure that the selection criteria aren't
#-- the same as what we're grouping on. If the candidate values
#-- of ?p are known in advance, this could replaced by
#-- `values ?p { :p :q ... }`.
filter(?p != rdf:type)
}
group by ?p ?o
如果我有这个数据:
:a :p :x; a :C .
:b :p :y; a :C .
:c :p :y; a :C .
:d :q :z; a :C .
:e :p :y; a :C .
如何对 select 第一个 ?s :p :y
进行 SPARQL 查询并排除任何其他。
换句话说,如何更改此查询:
SELECT *
WHERE {?s a :C}
所以在这个数据上,应该只有两个结果,:d
和 :b
或 :c
或 :e
。这无关紧要,因为它取决于与模式匹配的顺序,我想这不在查询的控制范围内。
注意:我正在简化实际情况,其中有一组模式,而不仅仅是 ?s a :C,但想法是,如果一些匹配的三元组也与 :p
链接,则应该是 ?s :p ?o
模式中相同 ?o
的一个结果。
您可以使用 LIMIT
将结果限制为只有一行:
SELECT ?s {
?s :p :y .
} LIMIT 1
您可以使用 ORDER BY
来影响结果的顺序,从而影响将要选择的资源。
您可以按 ?p 和 ?o 分组,然后从每组中的 ?s 值中抽样:
select (sample(?s_) as ?s) ?p ?o where {
?s_ a :C .
?s_ ?p ?o.
#-- Filter here is used to exclude the property that was used
#-- for selecting individuals (?s_ a :C). In general, this
#-- just needs to make sure that the selection criteria aren't
#-- the same as what we're grouping on. If the candidate values
#-- of ?p are known in advance, this could replaced by
#-- `values ?p { :p :q ... }`.
filter(?p != rdf:type)
}
group by ?p ?o