query-1301bbce7c2662ad7093e8aa2fbecb07
Handle different levels of date precisionHi all! The following query finds Tour de France participants, their team membership and name of the team in that year (both memberships of cyclists and team names can change over time). It's important to me that this works for the current year and past years, so there was some fiddling with undefined end time attributes. This works well for the current year (176 participants) with filter(?raceYear = 2019) However, team->official name->end times and person->membership->end times are often defined at the precision level "year", so a query that compares race dates with those end times would fail to include a person. In the following line, an ?et of 2004 would be evaluated as 2004-01-01 and the actual race end would be in 2004-07-28 (or something), so the filter unfortunately would be evaluated to false: filter(?st <= ?raceStart && if(bound(?et), ?et >= ?raceEnd, true)) For official team names I had the same problem and thus compared only the year of end times: filter(?stName <= ?raceStart && if(bound(?etName), year(?etName) >= year(?raceEnd), true)) This works a lot better, but this fails if the official team name end time is an exact day. In the following query, people of team Q766949 appear twice because the team name changed on a day in March of 2018 and so members are included twice for that year. Obviously, more precise information is a good thing, and we have to live with the fact that for a lot of other dates only the year is available. Do you know a way to change this query so that it takes into consideration a date's level of precision? How would I define a new end time variable as year(?et) + "-12-31" for an ?et with precision year, otherwise the exact ?et as found? I could then compare to that variable. Or maybe there is something more simple. This query is already quite long.
Use at
- https://query.wikidata.org/sparql
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT ?raceLabel ?team ?teamNameLabel ?person ?personLabel WHERE {
# person participated in a Tour de France race of a given year
?race wdt:P31 wd:Q33881.
?person wdt:P1344 ?race.
?race wdt:P580 ?raceStart.
?race wdt:P582 ?raceEnd.
bind(year(?raceStart) as ?raceYear)
filter(?raceYear = 2018)
# person's team membership
?person p:P54 ?teamStatement .
?teamStatement ps:P54 ?team .
optional {?teamStatement pq:P580 ?st}
optional {?teamStatement pq:P582 ?et}
filter(?st <= ?raceStart && if(bound(?et), year(?et) >= year(?raceEnd), true))
# team name at the time
?team p:P1448 ?nameStatement .
?nameStatement ps:P1448 ?teamName .
optional {?nameStatement pq:P580 ?stName}
optional {?nameStatement pq:P582 ?etName}
filter(?stName <= ?raceStart && if(bound(?etName), year(?etName) >= year(?raceEnd), true))
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} order by asc(?teamNameLabel) asc(?personLabel)