query-6733fb82973919939444d1bc644b3a58

rq turtle/ttl

Propertiesinstance of (P31)occupation (P106)subclass of (P279)date of birth (P569)date of death (P570)

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
PREFIX p: <http://www.wikidata.org/prop/>
#defaultView:BarChart
# Query to find all musicians who have already died 
# calculate their age (full years) at death
# count how many of them died at each age

SELECT ?age  (COUNT (DISTINCT ?a) AS ?count) WHERE {
  ?a wdt:P31 wd:Q5 . #instance of human
  ?a wdt:P106/wdt:P279 wd:Q639669 . #occupation a subclass of musician
  ?a p:P569/psv:P569 ?birth_date_node . 
  ?a p:P570/psv:P570 ?death_date_node .
  ?birth_date_node wikibase:timeValue ?birth_date .
  ?death_date_node wikibase:timeValue ?death_date .
  FILTER(?age > 10 && ?age < 100) . #ignore outlyers, several of which are probably errors
  BIND( year(?death_date) - year(?birth_date) - if(month(?death_date)<month(?birth_date)

Query found at