在sparql查询中按国家/地区使用组的城市总和

时间:2016-05-09 17:25:21

标签: sparql semantics

我想在Protege工具中使用SPARQL查询计算每个国家/地区的城市总数。城市与国家之间的关系是城市isLocatedIn国家。我尝试了下面给出的这个查询,但它失败了。所以请帮助我。

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 PREFIX ge:<http://www.semanticweb.org/waqas/ontologies/2016/4/untitled-ontology-15#>
SELECT  ( ?country SUM(?city) AS  ?TotalCity )
    WHERE {  ?city ge:isLocatedIn ?country . ?city rdf:type ?city} 
    GROUP BY(?country)

2 个答案:

答案 0 :(得分:2)

似乎是一个简单的语法问题。请参阅SPARQL 1.1规范中的Grouping and Aggregation

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX ge:<http://www.semanticweb.org/waqas/ontologies/2016/4/untitled-ontology-15#>
SELECT ?country (COUNT(?city) AS ?TotalCity)
WHERE {  
   ?city ge:isLocatedIn ?country . 
   ?city rdf:type ge:City .
} 
GROUP BY(?country)

答案 1 :(得分:-2)

回答我的问题

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX ge:<http://www.semanticweb.org/waqas/ontologies/2016/4/untitled-ontology-15#>
SELECT ?country (COUNT(?city) AS  ?TotalCity)
WHERE {  
   ?city ge:isLocatedIn ?country . 
   ?city rdf:type ge:City .
} 
GROUP BY(?country)