Search code examples
sparqlwikidata

Wikidata query does not give expected results


I want to get humans whose parents who are not married (the parents are not in each others spouse property), so I write the following query

SELECT ?human ?father ?mother ?someone ?humanLabel ?fatherLabel ?motherLabel ?someoneLabel WHERE {
  ?human wdt:P31 wd:Q5.
  ?human wdt:P22 ?father.
  ?human wdt:P25 ?mother.
  ?father wdt:P26 ?someone.
  FILTER (?mother != ?someone)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
 }limit 100

Then I discover if one person has several spouses, then the results give me lots of garbage. take Q15904745 as an example, his father(Q198042) has 2 spouse records, and his mother(Q16603522) is one of the spouse, but his info still will returns and i do not want to include this.

I would like to ask how can I change my query to get what I want? Here is the link to Wikidata endpoint.


Solution

  • If something like this:

        SELECT distinct ?human ?father ?mother ?someone ?humanLabel ?fatherLabel ?motherLabel 
    WHERE {
      ?human wdt:P31 wd:Q5.
      ?human wdt:P22 ?father.
      ?human wdt:P25 ?mother.
      FILTER (NOT EXISTS {?father wdt:P26 ?mother }) .
      FILTER (NOT EXISTS {?mother wdt:P26 ?father }) .  
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
     }
    order by ?humanLabel
    limit 100