1

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

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

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

4

1 回答 1

1

首先,查询非常接近。只需移动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 }
      }
   }
} 
于 2016-04-25T18:35:33.660 回答