Counting in SPARQL

冷暖自知 提交于 2020-01-01 08:43:22

问题


Ok so i have this query

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 

SELECT DISTINCT (COUNT(?instance) AS ?count) WHERE {
?instance a <http://dbpedia.org/ontology/Ambassador> . 
}

and the result is 286. Cool. Now I want to get the number of ambassadors that have http://dbpedia.org/property/name property. But

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 

SELECT DISTINCT (COUNT(?instance) AS ?count) WHERE {
?instance a <http://dbpedia.org/ontology/Ambassador> . 
?instance <http://dbpedia.org/property/name> ?name
}

results in 533 :(. So it is counting more because there are people which have this property one or more times. But how do I get the number of ambassadors that have this property regardless of how many times they have it. Can you do this in a single query?

Thanks.


回答1:


You might want to try this:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 

SELECT (COUNT(DISTINCT ?instance) AS ?count) WHERE {
?instance a <http://dbpedia.org/ontology/Ambassador>; 
          <http://dbpedia.org/property/name> ?name
}

It's giving me a result of 283, which might or might not be right :).



来源:https://stackoverflow.com/questions/5966410/counting-in-sparql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!