%META:TOPICPARENT{name="VirtTipsAndTricksGuide"}% ---+SPARQL Date Arithmetic Examples Collection The following collection presents examples of SPARQL Date arithmetic calculations. %TOC% ---++Sample listing of musicians that includes age at time of death SELECT ?s ?genre ?died ?born ( bif:datediff( 'year', xsd:dateTime( str(?born) ), xsd:dateTime( str(?died) ) ) ) AS ?age WHERE { { SELECT DISTINCT ?s ?genre ?died ?born FROM WHERE { ?s a ; ?genre ; ?died ; ?born . } LIMIT 20 } } View [[http://uriburner.com/c/IHWYXN][online]] the results of executing this query. ---++Sample listing of musicians (includes use of IF for data cleansing) that includes age at time of death SELECT ?person ?genre ?died ?born ( IF ( ( datatype (?born) in (xsd:dateTime, xsd:date) ) and ( datatype (?died) in (xsd:dateTime, xsd:date) ), bif:datediff( 'year', xsd:dateTime( str(?born) ), xsd:dateTime( str(?died) ) ), "error" ) ) AS ?age WHERE { { SELECT DISTINCT ?person ?genre ?died ?born FROM WHERE { ?person a ; ?genre ; ?died ; ?born . } ORDER BY DESC ( (?person) ) LIMIT 100 } } View [[http://uriburner.com/c/IHZRVY][online]] the results of executing this query. ---++Sample listing of musicians that includes birth date, death date, and age ordered by musicians entity rank SELECT DISTINCT ?person ?plabel ?genre ?glabel ?died ?born ( (?person) ) as ?rank ( IF ( ( datatype (?born) in (xsd:dateTime, xsd:date) ) and ( datatype (?died) in (xsd:dateTime, xsd:date) ), bif:datediff( 'year', xsd:dateTime( str(?born) ), xsd:dateTime( str(?died) ) ), "error" ) ) AS ?age WHERE { { SELECT DISTINCT ?person ?plabel ?genre ?glabel ?died ?born FROM WHERE { ?person a ; ?genre ; ?died ; rdfs:label ?plabel ; ?born . ?genre rdfs:label ?glabel . FILTER (lang(?plabel) = "en") FILTER (lang(?glabel) = "en") } ORDER BY DESC ( (?person) ) LIMIT 100 } } View [[http://uriburner.com/c/IGGP5I][online]] the results of executing this query. ---++Sample listing of musicians that includes average age at time of death, by genre SELECT ?genre, (avg(?age)) AS ?avg WHERE { { SELECT DISTINCT ?genre ?person (bif:datediff( 'year', xsd:dateTime( str(?born) ), xsd:dateTime( str(?died)))) as ?age WHERE { { SELECT DISTINCT ?person ?genre ?died ?born FROM WHERE { ?person a ; ?genre ; ?died ; ?born . FILTER ( datatype (?born) IN (xsd:dateTime, xsd:date) ) FILTER ( datatype (?died) IN (xsd:dateTime, xsd:date) ) } } } } } GROUP BY (?genre) ORDER BY DESC (?avg) LIMIT 100 View [[http://uriburner.com/c/IGGP5B][online]] the results of executing this query. ---++ Sample listing of musicians that includes average age at time of death with entity rank, by genre SELECT ?genre, (avg(?age)) AS ?avg, ( (?person) ) as ?rank WHERE { { SELECT DISTINCT ?genre ?person ( bif:datediff ( 'year', xsd:dateTime( str(?born) ), xsd:dateTime( str(?died) ) ) ) as ?age WHERE { { SELECT DISTINCT ?person ?genre ?died ?born FROM WHERE { ?person a ; ?genre ; ?died ; ?born . FILTER ( datatype (?born) IN (xsd:dateTime, xsd:date) ) FILTER ( datatype (?died) IN (xsd:dateTime, xsd:date) ) } } } ORDER BY DESC ( (?person) ) } } ORDER BY DESC (?avg) LIMIT 100 View [[http://uriburner.com/c/IHWYXK][online]] the results of executing this query. ---++ Sample listing of musicians that includes average age at time of death with entity rank, by genre and Pretty Labels SELECT ?genre, str(?glabel) as ?genre_names, (avg(?age)) AS ?avg, ( (?person) ) as ?rank WHERE { { SELECT DISTINCT ?genre ?person ?glabel ( IF ( ( datatype (?born) in (xsd:dateTime, xsd:date) ) and ( datatype (?died) in (xsd:dateTime, xsd:date) ), bif:datediff('year',xsd:dateTime(str(?born)),xsd:dateTime(str(?died))), "error" ) ) as ?age WHERE { { SELECT DISTINCT ?person ?genre ?died ?born ?glabel FROM WHERE { ?person a ; ?genre ; ?died ; ?born . FILTER ( datatype (?born) IN (xsd:dateTime, xsd:date) ) FILTER ( datatype (?died) IN (xsd:dateTime, xsd:date) ) ?genre rdfs:label ?glabel . FILTER (?born < ?died) . FILTER ( lang(?glabel) = "en" ) } } } ORDER BY DESC ( (?person) ) } } ORDER BY DESC (?avg) LIMIT 100 View [[http://uriburner.com/c/86SSZ][online]] the results of executing this query. ---++ Sample listing of musicians that includes Entity Rank, Grouping and Pretty Labels SELECT DISTINCT ?person str(?plabel) ?genre str(?glabel) ?died ?born ( (?person) ) as ?rank ( IF ( ( datatype (?born) in (xsd:dateTime, xsd:date) ) and ( datatype (?died) in (xsd:dateTime, xsd:date) ), bif:datediff('year',xsd:dateTime(str(?born)),xsd:dateTime(str(?died))), "error" ) ) AS ?age WHERE { { SELECT DISTINCT ?person ?plabel ?genre ?glabel ?died ?born FROM WHERE { ?person a ; ?genre ; ?died ; rdfs:label ?plabel ; ?born . ?genre rdfs:label ?glabel . FILTER ( lang(?plabel) = "en" ) FILTER ( lang(?glabel) = "en" ) } ORDER BY DESC ( (?person) ) LIMIT 100 } } View [[http://uriburner.com/c/IGGP4Z][online]] the results of executing this query. ---++Related * [[VirtTipsAndTricksGuide][Virtuoso Tips and Tricks Collection]]