2

I am trying to adapt the Mona Lisa exercise in the Wikidata SPARQL tutorial so it will show all the materials used and where they apply.

If I simply do:

SELECT ?painting ?paintingLabel ?material ?materialLabel 
WHERE
{
  ?painting p:P186 [ ps:P186 ?material].
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
  FILTER (?painting = wd:Q12418)
}

Link to this query.

then I will see the 3 materials that make up the painting.

If I now edit that query to include P518 (applies to part), I only see the 2 materials that have the P518 qualifier:

SELECT ?painting ?paintingLabel ?material ?materialLabel ?appliesTo ?appliesToLabel
WHERE
{
  ?painting p:P186 [ ps:P186 ?material; pq:P518 ?appliesTo].
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
  FILTER (?painting = wd:Q12418)
}

Link to this query

How can I handle the fact that the P518 qualifier is optional?

2
SELECT ?painting ?paintingLabel ?material ?materialLabel ?appliesTo ?appliesToLabel {
  VALUES (?painting) {(wd:Q12418)}
  ?painting p:P186 ?statement .
  ?statement ps:P186 ?material 
  OPTIONAL { ?statement pq:P518 ?appliesTo }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]" }
}
  • Fantastic, thanks. I noticed you changed the FILTER (?painting = wd:Q12418) to VALUES (?painting) {(wd:Q12418)}. What is the difference between these two approaches? – Alan Buxton Apr 11 at 10:43
  • 2
    @AlanBuxton, VALUES is more conventional way for providing inline data: see w3.org/TR/sparql11-query/#inline-data In general, optimizer can rewrite a query in order to avoid checking all solutions via FILTER, but not always. – Stanislav Kralin Apr 11 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.