I am trying to gather information on castles, palaces and other types of grand houses in the Netherlands and have written the following Wikidata query, which includes several optional properties as the extent of the available information varies from item to item:
SELECT DISTINCT ?item ?itemLabel ?type ?nativelabel ?architect ?stlye ?date ?starttime ?opening ?owner ?admin ?GeoNamesID ?coords WHERE {
?item (wdt:P17/(wdt:P131*)) wd:Q55;
wdt:P31/wdt:P279 ?type.
FILTER((?type = wd:Q23413) || (?type = wd:Q16560) || (?type = wd:Q879050) || (?type = wd:Q751876) || (?type = wd:Q12292478) || (?type = wd:Q16823155))
OPTIONAL {
?item wdt:P31 ?type;
wdt:P131 ?admin;
wdt:P1566 ?GeoNamesID;
wdt:P1705 ?nativelabel;
wdt:P127 ?owner;
wdt:P571 ?date;
wdt:P580 ?starttime;
wdt:P1619 ?opening;
wdt:P84 ?architect;
wdt:P149 ?style;
wdt:P635 ?coords.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
LIMIT 3000
The query returns 1212 results, which are all relevant, but the optional properties are being ignored. The only information that is returned are item
, itemLabel
, and type
. As I am not a regular SPARQL user, I cannot spot the mistake in my query and would very much appreciate your help.
You only get a result if all of the graph patterns in the OPTIONAL
block match.
If you want to match each pattern on its own, you have to place each pattern in its own OPTIONAL
block:
OPTIONAL {?item wdt:P31 ?type .}
OPTIONAL {?item wdt:P131 ?admin .}
OPTIONAL {?item wdt:P1566 ?GeoNamesID .}
OPTIONAL {?item wdt:P1705 ?nativelabel .}
OPTIONAL {?item wdt:P127 ?owner .}
OPTIONAL {?item wdt:P571 ?date .}
OPTIONAL {?item wdt:P580 ?starttime .}
OPTIONAL {?item wdt:P1619 ?opening .}
OPTIONAL {?item wdt:P84 ?architect .}
OPTIONAL {?item wdt:P149 ?style .}
OPTIONAL {?item wdt:P635 ?coords .}