5

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.

AndW99
  • 552
  • 3
  • 12
  • Looking at [the data](http://dbpedia.org/page/Alan_Shearer) , the goals do not seem to be bound to clubs by anything apart from their order of appearance. As far as I can tell there are no explicit RDF statements binding one to the other. Accessing values by index is quite a pain in RDF. – toniedzwiedz Apr 19 '15 at 09:41
  • Thanks for the reply, looking at other data, the order of appearance does not always correlate - but of course the data always appears correctly in Wikipedia. My original idea was to obtain specific information from a Wiki infobox whilt avoiding parsing the entire infobox. – AndW99 Apr 19 '15 at 11:06

1 Answers1

2

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

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thanks, I understand the issue now, will have to go back and think of a better approach and data set. – AndW99 Apr 19 '15 at 11:29
  • 1
    @AndW99 What do you mean? Doesn't this solve the issue? (Sorry, I posted an answer first that explained the issues that the comments were discussing, but then realized that DBpedia *does* have the data you want, and the association between teams and goals, but you have to query just a little bit differently.) – Joshua Taylor Apr 19 '15 at 11:30
  • apologies, your edited response was provided as I was replying to your original answer! Yes your answer here is what I was looking for. Thanks - have up-voted your answer – AndW99 Apr 19 '15 at 11:36
  • 1
    Neat, I didn't see that. By the way, what's the point of having the multiple values for goals as `dbpprop:goals` then? All of the information contained by these properties is made available (along with additional context) by the resources you mentioned in your answer. I get the point of having aggregates materialized somewhere as a possible performance improvement (like the `dbprop:totalgoals`) but I fail to see the reason behind having these three individual numbers, taken out of context, at the level of `dbpedia:Alan_Shearer`. Is there something I'm missing? – toniedzwiedz Apr 19 '15 at 11:59
  • 1
    @toniedzwiedz Remember, all the content in DBpedia is *extracted* from Wikipedia. Usually, an infobox on a page tells you something about that particular resource. (E.g., a Person infobox with an age field usually tells you age of the person.) I imagine that in this case, there's some sort of Station infobox that's actually telling you something about something that's not quite the same as the resource. E.g., multiple Station infoboxes could each have a "goals" property. The naive infobox extraction would just associate these with the resource. DBpedia has two types of information: – Joshua Taylor Apr 19 '15 at 12:23
  • @toniedzwiedz The raw infobox data, which uses the dbpprop properties, and a more sophisticated (and much cleaner) infobox ontology, which uses the dbpedia-owl properties, and has more advanced mappings to normalize values and, I guess, in this case, structure the data with some auxiliary objects. You can read more about the difference on the DBpedia page; see [4.3. Infobox Data](http://wiki.dbpedia.org/Datasets#h434-10). Generally the infobox ontology data is much cleaner. Note that the original query in the question was using dbpprop:goals (raw infobox data) whereas my answer uses – Joshua Taylor Apr 19 '15 at 12:26
  • @toniedzwiedz dbpedia-owl:goals (on the value of the dbpedia-owl:careerStation property). The dbpedia-owl properties have much better structure, and cleaner data. – Joshua Taylor Apr 19 '15 at 12:27