0

I would like to write an equivalent recursive query in Sparql to query all types of organisations on Wikidata that roll up to wd:43229 (organisation)

For example, the following entity Q4926947, The output should like so

entity|entityLabel|path

Q4926947|Blitz Arcade| Q210167->Q112042224->Q1058914->Q4830453->Q43229
Q4926947|Blitz Arcade| Q210167->Q112042224->Q783794->Q43229
Q4926947|Blitz Arcade| Q210167->Q112042224->Q18388277->Q6881511->Q4830453->Q43229
Q4926947|Blitz Arcade| Q210167->Q112042224->Q18388277->Q6881511->Q362482->Q679206->Q43229

There are several paths that lead to Q43229. In my query, I would only like to specify the root (Q43229) and it should be able to query all the leaf nodes that link to Q43229

This is what I've got so far, but it is far from the desired result. Any help is appreciated

SELECT ?item ?itemLabel  (group_concat(?linkTo; separator=",") as ?org_path) {
  wd:Q43229 ^wdt:P279* ?item
  OPTIONAL { ?item ^wdt:P279* ?linkTo }
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
} group by ?item ?itemLabel
limit 5
sl_
  • 1
  • 1
  • 1
    honestly, SPARQL is the wrong language to get paths as there is no proper concept of paths as bindings aka resultsets. The clostest you can get are suggestions like here: https://stackoverflow.com/questions/18024413/finding-all-steps-in-property-path - but this is still limited and indeed far away from your intended result format. Especially in `group_concat` there is no guarantee of the order of the concatenated results. – UninformedUser Oct 04 '22 at 04:55
  • The alternative is to make use of this Blazegraph extension: https://github.com/blazegraph/database/wiki/RDF_GAS_API - at least it's efficient – UninformedUser Oct 04 '22 at 04:56
  • @UninformedUser Do you know whether the GAS API traverses all paths from a given node to its parent? For example municipal service has 2 paths that lead to organisations: 1) Q3190900 -> organisation. 2) department -> organizational subdivision -> organisation The API returns only the first result. Is there a way to include all paths? – sl_ Oct 11 '22 at 14:16

0 Answers0