查询
听起来您想要一个类似的查询:
SELECT ?philosopher ?pName ?influence (SAMPLE(?iName) as ?iName)
WHERE {
# This subquery selects all the philosophers and
# selects just one of their names .
{
SELECT ?philosopher (SAMPLE(?pName) as ?pName) WHERE {
?philosopher a dbpedia-owl:Philosopher ;
foaf:name ?pName .
}
GROUP BY ?philosopher
}
# This main query selects the influence of the
# philosophers and select their names. The GROUP
# BY on the outer query puts all the
# (?philosopher,?pName,?influence,?iName) tuples
# that have the same ?philosopher, ?pName, and
# influence together, and the (SAMPLE(?iName) as ?iName)
# in the outer SELECT combines them all, choosing an
# arbitrary representative ?iName.
?influence dbpedia-owl:influenced ?philosopher ;
a dbpedia-owl:Philosopher ;
foaf:name ?iName .
}
GROUP BY ?philosopher ?pName ?influence
SPARQL 结果
如果你只对名称感兴趣,而不关心选择实际资源,你不需要?philosopher
和?influence
在最外层SELECT
,可以做到
SELECT ?pName (SAMPLE(?iName) as ?iName)
WHERE { …
SPARQL 结果
您可能还想ORDER BY
在最后添加一个以使结果更容易检查:
…
GROUP BY ?philosopher ?pName ?influence
ORDER BY ?pName
SPARQL 结果
对于柏拉图来说,这些最后的结果包括以下行:
"Plato"@en "Socrates"@en
"Plato"@en "Parmenides"@en
"Plato"@en "Zeno of Elea"@en
"Plato"@en "Pythagoras"@en
"Plato"@en "Gorgias"@en
"Plato"@en "Protagoras"@en
"Plato"@en "Heraclitus"@en
在我在这里编写的查询中,我曾经任意SAMPLE
选择一个哲学家的s,但是聚合代数foaf:name
中还有其他函数可以用来选择一个值。 如果您希望按顺序排列“第一个”值,您可能会对Min感兴趣。
子查询、GROUP BY 和 SAMPLE、MIN 等。
这实际上与第 12 节SPARQL 规范的子查询中给出的子查询示例非常相似。在该示例中,以下查询用于选择 Alice 认识的人,并且对于每个人,只选择一个人的姓名:
PREFIX : <http://people.example/>
SELECT ?y ?minName
WHERE {
:alice :knows ?y .
{
SELECT ?y (MIN(?name) AS ?minName)
WHERE {
?y :name ?name .
} GROUP BY ?y
}
}
这不难适应哲学影响问题。哲学家问题首先选择所有哲学家及其姓名,按实际哲学家资源分组,并使用样本为每个哲学家选择一个代表姓名。外部查询也是如此,但不是选择哲学家,而是选择影响每个哲学家的实体。将结果分组并选择影响的代表名称。