在结果中查询具有聚合计数和图形的多个图形

时间:2016-04-25 17:40:30

标签: sparql fuseki

是否可以计算多个命名图中三元组的出现次数并将结果作为表中的行返回?如:

   ?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 ...

因为可能有数百个图表需要查询。

1 个答案:

答案 0 :(得分:1)

首先,查询非常接近。只需移动图表语句中的SELECT - 基本上说明每个图表,找到这些汇总值&#39;。其次,如果?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 }
      }
   }
}