SPARQL - query a property and return results for a related property

牧云@^-^@ 提交于 2019-12-10 06:06:23

问题


I am new to SPARQL and I am trying to run a SPARQL query so that I return the results for a property and list against this the value for a related property.

Example code being:

SELECT ?player ?position ?club ?goals WHERE {
  ?player a <http://dbpedia.org/ontology/SoccerManager> . filter (contains (str(?player), "Alan_Shearer")) .
  ?player <http://dbpedia.org/ontology/position> ?position .
  ?player <http://dbpedia.org/property/clubs> ?club .
  ?player <http://dbpedia.org/property/goals> ?goals .
}

With the result being all goals replicated against each club:

player  position    club    goals
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Southampton_F.C.    23
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Southampton_F.C.    112
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Southampton_F.C.    148
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Newcastle_United_F.C.   23
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Newcastle_United_F.C.   112
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Newcastle_United_F.C.   148
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Blackburn_Rovers_F.C.   23
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Blackburn_Rovers_F.C.   112
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Blackburn_Rovers_F.C.   148

The goals per club is associated correctly in the data set, and so what I want to obtain is only the goals for the respective club:

player  position    club    goals
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Southampton_F.C.    23
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Newcastle_United_F.C.   112
http://dbpedia.org/resource/Alan_Shearer    http://dbpedia.org/resource/Forward_(association_football)  http://dbpedia.org/resource/Blackburn_Rovers_F.C.   148

However, I don't follow how to do this in SPARQL, any assistance is greatly appreciated.


回答1:


Note in the data that there are some dbpedia-owl:careerStation property values:

dbpedia-owl:careerStation dbpedia:Alan_Shearer__1, dbpedia:Alan_Shearer__2, ...

If you look at the value of those properties, e.g., http://dbpedia.org/page/Alan_Shearer__3, you can see that some of them have a number of goals property. That means you can do this:

select ?player ?position ?team ?goals {
  values ?player { dbpedia:Alan_Shearer }
  ?player dbpedia-owl:position ?position ;
          dbpedia-owl:careerStation [ dbpedia-owl:team ?team ;
                                      dbpedia-owl:numberOfGoals ?goals ] .
}

SPARQL results

Since not all the stations have goals information, you might want to use an optional here to get the station and then the goals if available:

select ?player ?position ?team ?goals {
  values ?player { dbpedia:Alan_Shearer }
  ?player dbpedia-owl:position ?position ;
          dbpedia-owl:careerStation ?station .
  ?station dbpedia-owl:team ?team .
  optional { ?station dbpedia-owl:numberOfGoals ?goals }
}

SPARQL results



来源:https://stackoverflow.com/questions/29727984/sparql-query-a-property-and-return-results-for-a-related-property

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