首页 > 解决方案 > SPARQL:排除 COUNT 中的双重资源

问题描述

我正在尝试编写一个 SPARQL 查询,它只计算每所大学的荷兰政治家人数。然而,对于每个 dbo:almaMater,对于一些政治家来说,有一个额外的资源(例如他们的专业,dbr:Sociology)。这也反映在 COUNT 中。例如。我得到莱顿大学的计数 49,这应该只有 42。知道如何解决这个问题吗?我已经尝试过 FILTER NOT EXISTS 和 MINUS 但两者都对计数没有任何作用。谢谢你。

我的查询:

SELECT distinct  ?education (COUNT(?education) AS ?edu_count) 
WHERE { ?name <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Person>.
        ?name <http://dbpedia.org/ontology/birthPlace> <http://dbpedia.org/resource/Netherlands>.
        ?name <http://dbpedia.org/ontology/party> ?party.
        ?name <http://dbpedia.org/ontology/almaMater> ?education.
        ?education <http://dbpedia.org/ontology/type> <http://dbpedia.org/resource/Public_university>.

        
} GROUP BY ?education 
ORDER BY DESC(?edu_count)

标签: sparqlrdfdbpediaturtle-rdf

解决方案


修改后的查询,基于评论,为了清楚起见,有额外的空格。查询形式结果——

SELECT                               ?education
       ( COUNT ( DISTINCT ?name ) AS ?cnt )
WHERE { ?name      <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Person> .
        ?name      <http://dbpedia.org/ontology/birthPlace>          <http://dbpedia.org/resource/Netherlands> .
        ?name      <http://dbpedia.org/ontology/party>               ?party .
        ?name      <http://dbpedia.org/ontology/almaMater>           ?education .
        ?education <http://dbpedia.org/ontology/type>                <http://dbpedia.org/resource/Public_university> .
      } 
GROUP BY ?education 
ORDER BY DESC ( ?cnt ) 
         ASC ( ?education )

推荐阅读