简体   繁体   中英

SPARQL - AND & OR Filters on Literal

I am trying to understand how to do filtering in SPARQL with combined AND and OR conditions.

I try to find all physicists living within Newton's lifetime via the Wikidata Query Service (query.wikidata.org). This is my query:

SELECT ?p1 ?p1Label ?p1t1 ?p1t2 ?p2 ?p2Label ?p2t1 ?p2t2
WHERE {
  FILTER (?p1=wd:Q935) .    # Newton
  ?p1 wdt:P569 ?p1t1 .      # date of birth
  ?p1 wdt:P570 ?p1t2 .      # date of death
  ?p2 wdt:P106 wd:Q169470 . # physicist
  ?p2 wdt:P569 ?p2t1 .      # date of birth
  ?p2 wdt:P570 ?p2t2 .      # date of death
  { FILTER (xsd:dateTime(?p2t1) > xsd:dateTime(?p1t1))
  . FILTER  (xsd:dateTime(?p2t1) < xsd:dateTime(?p1t2)) }
  UNION
  { FILTER (xsd:dateTime(?p2t2) > xsd:dateTime(?p1t1))
  . FILTER (xsd:dateTime(?p2t2) < xsd:dateTime(?p1t2)) }
  UNION
  { FILTER (xsd:dateTime(?p2t1) < xsd:dateTime(?p1t1))
  . FILTER (xsd:dateTime(?p2t2) > xsd:dateTime(?p1t2)) } .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
ORDER BY xsd:dateTime(?p2t1)

The query can be processed but does not yield any results.

Each one of the three { FILTER . FILTER } blocks works fine when used without the others.

What am I doing wrong?

{ FILTER (xsd:dateTime(?p2t1) > xsd:dateTime(?p1t1))
  . FILTER  (xsd:dateTime(?p2t1) < xsd:dateTime(?p1t2)) }

evaluates to empty.

UNION does not "or" them together -- || does that.

Each branch of the UNION is a separate graph pattern.

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