是否可以计算多个命名图中三元组的出现次数并将结果作为表中的行返回?如:
?g ?count ?sequence_count
-------- ------- ---------------
graph1 54 54
graph2 120 80
这是我尝试过的查询。
SELECT ?g ?count ?sequence_count
FROM NAMED <graph1>
FROM NAMED <graph2>
WHERE {
{
select (COUNT(?identifier) as ?count) (COUNT(?sequence) as ?sequence_count)
WHERE { GRAPH ?g {
?identifier a <http://www.w3.org/2000/01/rdf-schema#Resource> .
OPTIONAL { ?identifier <urn:sequence> ?sequence }
} }
}
}
但结果是:
?g ?count ?sequence_count
-------- ------- ---------------
174 134
我试图避免写出来:
select ?count_graph1 ?sequence_count_graph1 ?count_graph2 ...
因为可能有数百个图形可供查询。
首先,查询非常接近。 只需将SELECT
移动到图形语句内部 - 基本上是声明"对于每个图形,找到这些聚合值"。 其次,如果任何?identifier
匹配项具有多个值,则?identifier
的计数将具有重复项,因此需要DISTINCT
结果。 请尝试以下操作:
SELECT *
FROM NAMED <graph1>
FROM NAMED <graph2>
WHERE {
GRAPH ?g {
SELECT (COUNT(DISTINCT ?identifier) as ?count) (COUNT(?sequence) as ?sequence_count)
WHERE {
?identifier a <http://www.w3.org/2000/01/rdf-schema#Resource> .
OPTIONAL { ?identifier <urn:sequence> ?sequence }
}
}
}