简体   繁体   中英

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

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.

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, eg, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM