Search code examples
recursionuniquesparqlwikidata

Show all Wikidata descendants, uniquely


I have the following working query with the aim of showing all subclasses of Q3314483 [1]:

SELECT ?item (SAMPLE(?itemLabel) AS ?itemLabel) (SAMPLE(?subclass) as ?subklass) (SAMPLE(?subclassLabel) AS ?subLabel) WHERE {
   ?item wdt:P279* wd:Q3314483 ;
         wdt:P279 ?subclass .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
group by ?item

The SAMPLE and GROUP pattern is designed to make the items unique. It seems to work, however the label columns are blank. How can they be displayed?

  1. Based on this

Solution

  • Thanks to @AKSW in the comments. Answer is:

    SELECT ?item (SAMPLE(?itemLabel) AS ?itemLabel) (SAMPLE(?subclass) as ?subklass) (SAMPLE(?subclassLabel) AS ?subLabel) WHERE {
      ?item wdt:P279* wd:Q3314483 ;
      wdt:P279 ?subclass . SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". ?subclass rdfs:label ?subclassLabel. ?item rdfs:label ?itemLabel.
      }
    }
    GROUP BY ?item