RDF date range queries in SPARQL

February 20, 2015

Let's explore how to query an RDF triple store for births within a date range. We'll use information from Wikidata, and make a couple of assumptions:

Let's find Feynman's birth date:

prefix wdq: <http://www.wikidata.org/entity/>
select ?x WHERE {
  wdq:Q39246 wdq:P569s ?x
}
x
wdq:Q39246SDD586298-5665-41DC-B11E-0081CD334192

This means there exists a statement:

wdq:Q39246 wdq:P569s wdq:Q39246SDD586298-5665-41DC-B11E-0081CD334192

Let's find out about wdq:Q39246SDD586298-5665-41DC-B11E-0081CD334192:

prefix wdq: <http://www.wikidata.org/entity/>
select ?x ?y WHERE {
  wdq:Q39246SDD586298-5665-41DC-B11E-0081CD334192 ?x ?y
}
x y
<http://www.w3.org/ns/prov#wasDerivedFrom> <http://www.wikidata.org/entity/R08e32879ba370024b9573ee2b7b93071>
<http://www.w3.org/ns/prov#wasDerivedFrom> <http://www.wikidata.org/entity/R98fa0a037ed1305854d6862377367765>
<http://www.wikidata.org/entity/P569v> <http://www.wikidata.org/entity/VT341f87fb553966c9e67c7968d6f778e8>
rdf:type <http://www.wikidata.org/ontology#Statement>

That value for P569v looks interesting. Let's check it out:

prefix wdq: <http://www.wikidata.org/entity/>
select ?x ?y WHERE {
  wdq:VT341f87fb553966c9e67c7968d6f778e8 ?x ?y
}
x y
<http://www.wikidata.org/ontology#preferredCalendar> <http://www.wikidata.org/entity/Q1985727>
<http://www.wikidata.org/ontology#time> 1918-05-11
<http://www.wikidata.org/ontology#timePrecision> 11
rdf:type <http://www.wikidata.org/ontology#TimeValue>

Looks like Feynman was born on May 11, 1918.

Let's find everyone born within a month of that date:

prefix wdq: <http://www.wikidata.org/entity/>
prefix wdo: <http://www.wikidata.org/ontology#>
prefix xs: <http://www.w3.org/2001/XMLSchema#>
select ?entity ?date WHERE {
  ?entity ?relatedTo ?dateS .
  ?dateS wdq:P569v ?dateV .
  ?dateV wdo:preferredCalendar wdq:Q1985727 .
  ?dateV wdo:time ?date .
  FILTER (?date > "1918-04-11"^^xs:date && ?date < "1918-06-11"^^xs:date)
}
entity date
<http://www.wikidata.org/entity/Q552288> 1918-05-18
<http://www.wikidata.org/entity/Q1272841> 1918-04-12
<http://www.wikidata.org/entity/Q1898621> 1918-05-08
<http://www.wikidata.org/entity/Q2528848> 1918-05-08
<http://www.wikidata.org/entity/Q2667591> 1918-05-18
... ...

With the most recent Wikidata dump, 1098 results are returned.

References