query-1301bbce7c2662ad7093e8aa2fbecb07

rq turtle/ttl

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

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)

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v8("?et") v5("?etName") v14("?nameStatement") v11("?person"):::projected v2("?personLabel"):::projected v10("?race") v6("?raceEnd") v4("?raceStart") v12("?raceYear") v7("?st") v3("?stName") v13("?team"):::projected v15("?teamName") v1("?teamNameLabel"):::projected v12("?teamStatement") c17(["en"]):::literal c15(["bd:serviceParam"]):::iri c4(["wd:Q33881"]):::iri f0[["?stName <= ?raceStartif(bound(?etName),year-from-dateTime(?etName) >= year-from-dateTime(?raceEnd),'true^^xsd:boolean')"]] f0 --> v3 f0 --> v4 f0 --> v5 f0 --> v6 f1[["?st <= ?raceStartif(bound(?et),year-from-dateTime(?et) >= year-from-dateTime(?raceEnd),'true^^xsd:boolean')"]] f1 --> v7 f1 --> v4 f1 --> v8 f1 --> v6 f2[["?raceYear = '2018^^xsd:integer'"]] f2 --> v12 v10 --"p:direct/P31"--> c4 v11 --"p:direct/P1344"--> v10 v10 --"p:direct/P580"--> v4 v10 --"p:direct/P582"--> v6 bind3[/"year-from-dateTime(?raceStart)"/] v4 --o bind3 bind3 --as--o v12 v11 --"p:P54"--> v12 v12 --"p:statement/P54"--> v13 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v12 -."p:qualifier/P580".-> v7 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v12 -."p:qualifier/P582".-> v8 end v13 --"p:P1448"--> v14 v14 --"p:statement/P1448"--> v15 subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v14 -."p:qualifier/P580".-> v3 end subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; v14 -."p:qualifier/P582".-> v5 end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c15 --"wikibase:language"--> c17 end