0

Query: select instance of human whose profession is singer/musician

  • wd:Q177220 (Singer)
  • wd:Q639669 (Musician)
SELECT DISTINCT ?human ?humanLabel ?genre ?genreLabel WHERE {
  VALUES ?professions {
    wd:Q177220
    wd:Q639669
  }
  ?human wdt:P31 wd:Q5;
    wdt:P106 ?professions;
    wikibase:statements ?statementcount.
  FILTER(?statementcount > 50 )
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  OPTIONAL { ?human wdt:P136 ?genre. }
}
ORDER BY (?humanLabel)
LIMIT 50

Query link

How to group query by optional param genre so that it will return something like this ["rock", "pop-rock"]

Puni
  • 1,214
  • 2
  • 18
  • 34
  • you#re not grouping by the genre but the `?human` of course. And then do `group_concat` for the genre – UninformedUser Feb 24 '20 at 07:15
  • 1
    `SELECT DISTINCT ?human (sample(?humanLabel) as ?l) (group_concat(distinct ?genre; separator= ",") as ?genres) (group_concat(distinct ?genreLabel; separator= ",") as ?genreLabels) WHERE { VALUES ?professions { wd:Q177220 wd:Q639669 } ?human wdt:P31 wd:Q5; wdt:P106 ?professions; wikibase:statements ?statementcount. FILTER(?statementcount > 50 )OPTIONAL { ?human wdt:P136 ?genre. } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". ?genre rdfs:label ?genreLabel . ?human rdfs:label ?humanLabel . } } group by ?human ORDER BY (?humanLabel) LIMIT 50` – UninformedUser Feb 24 '20 at 07:27
  • @UninformedUser Im getting bad aggregate error when adding more optional param OPTIONAL { ?human wdt:P27 ?country_of_citizenship. } – Puni Feb 24 '20 at 08:39
  • for all non-grouping variables you have to apply some aggregate function, e.g. `sample` to get just one random value out of possible many or do `group_concat` to show all as a string. That's clearly what I did in my query. – UninformedUser Feb 24 '20 at 08:41
  • Sorry for my ignorance got it working. Thank you. – Puni Feb 24 '20 at 09:17

0 Answers0