SPARQL query where at least one of multiple properties exist

45 views Asked by At

I'm attempting to run a query on a custom Wikibase instance. In this Wikibase, there is an identifier that has multiple versions (v0, v1, v2, and v3). I'm attempting to return all of the entities which have at least one of those identifiers present. I thought that the OPTIONAL keyword might work so I ran the following:

PREFIX wd: <https://wikibase.cloud/entity/>
PREFIX wdt: <https://wikibase.cloud/prop/direct/>

SELECT ?item ?itemLabel ?val_0 ?val_1 ?val_2 ?val_3
WHERE 
{
  OPTIONAL { ?item wdt:P1 ?val_0 . }
  OPTIONAL { ?item wdt:P2 ?val_1 . }
  OPTIONAL { ?item wdt:P3 ?val_2 . }
  OPTIONAL { ?item wdt:P4 ?val_3 . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

However, this only returns entities that have a v0 identifier (?val_0) and then performs the OPTIONAL command on the subsequent versions. For instance, if the entity has only v1 (?val_1) it should still return. If the entity has only v3 (?val_3) it should still return.

I can't find anything regarding a similar query via Google searching or via searching StackOverflow, but then again I might not be searching the correct terminology.

To illustrate, this is a mockup of the current output:

item itemLabel val_0 val_1 val_2 val_3
Q1 Label A X
Q2 Label B X X
Q3 Label C X X X
Q4 Label D X X X X
Q5 Label E X X

And this is a mockup of the expected output:

item itemLabel val_0 val_1 val_2 val_3
Q1 Label A X
Q2 Label B X
Q3 Label C X X
Q4 Label D X X X
Q5 Label E X X

Any help or pointers in the right direction would be much appreciated! Thanks so much!

1

There are 1 answers

1
Stanislav Kralin On BEST ANSWER
  1. Add ?item wdt:P1|wdt:P2|wdt:P3|wdt:P4 [] in the beginning of your query.
  2. Never start your query with OPTIONAL.