在结果中使用聚合计数和图形查询多个图形

Querying multiple graphs with aggregate Count and graph in results

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

   ?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 移动到图形语句中 - 基本上是说明 'for each graph, find these aggregate values'。其次,如果任何 ?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 }
      }
   }
}