简体   繁体   中英

Query for age in sparql

I am writing a query that calculates the age of someone knowing their birthdate and deathdate. I want to ask for a certain artist (in this case Michael Jackson). The problem is that I can't reuse the birthdate and deathdate in the following query:

select ?artist ?age  
where { 
  dbr:Michael_Jackson dbo:birthDate ?birthdate .

  dbr:Michael_Jackson dbo:deathDate ?deathdate . 

  bind( year(?deathdate) - year(?birthdate) - if(month(?deathdate)<month(?birthdate) || (month(?deathdate)=month(?birthdate) && day(?deathdate<day(?birthdate)),1,0) as ?age)
}

Anyone knows how to solve this problem?

I edited the query a bit to get the values you're calculating against --

birthdate                                              deathdate
"1958-8-29"^^<http://www.w3.org/2001/XMLSchema#date>   "2009-6-25"^^<http://www.w3.org/2001/XMLSchema#date>

The error you're hitting is because these are not valid xsd:date literals -- which require 2 digits for the month.

This data issue has been fixed on Wikipedia and so on DBpedia-Live, and your query works there, as you can see . There are other issues with DBpedia-Live data (such as multiple versions of the abstract being shown, when you really only want to see the latest), but perhaps this solves your immediate need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM