Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sparql

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.

like image 211
AndW99 Avatar asked Nov 01 '22 06:11

AndW99


1 Answers

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

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

like image 130
Joshua Taylor Avatar answered Jan 04 '23 14:01

Joshua Taylor