Jonas Sourlier
Jonas Sourlier

Reputation: 14435

How to do `FILTER (x IN ( ... ) )` in SPARQL?

I'm trying to load all the subtaxons of Biota/Q2382433 (i.e. entities with P271 "parent taxon" pointing to Q2382433) in Wikidata.

The following query works fine:

SELECT ?item ?itemLabel
(GROUP_CONCAT(DISTINCT ?class; SEPARATOR=", ") AS ?classes)
WHERE {
    ?class wdt:P171 ?item.
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    BIND(wd:Q2382443 AS ?item)
}
GROUP BY ?item ?itemLabel

With FILTER instead of BIND, it yields zero results:

SELECT ?item ?itemLabel
(GROUP_CONCAT(DISTINCT ?class; SEPARATOR=", ") AS ?classes)
WHERE {
    ?class wdt:P171 ?item.
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    FILTER(?item in (wd:Q2382443))
}
GROUP BY ?item ?itemLabel

How can I get the query to work using FILTER?

Upvotes: 0

Views: 68

Answers (1)

Teh
Teh

Reputation: 3306

For some reason on Wikidata when itemLabel is added to the SELECT clause, the filter won't work. However, if you remove it, the query works fine.

You could also redeclare the triple pattern with a new variable name and filter on it:

SELECT ?item ?itemLabel
(GROUP_CONCAT(DISTINCT ?class; SEPARATOR=", ") AS ?classes)
WHERE {
    ?class wdt:P171 ?item .
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }

    ?class wdt:P171 ?parentTaxon .
    FILTER(?parentTaxon IN (wd:Q2382443))
}
GROUP BY ?item ?itemLabel

Results

Edit: VALUE also works:

SELECT ?item ?itemLabel
(GROUP_CONCAT(DISTINCT ?class; SEPARATOR=", ") AS ?classes)
WHERE {
    ?class wdt:P171 ?item .
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    VALUES ?item { wd:Q2382443 }
}
GROUP BY ?item ?itemLabel

Results

Upvotes: 3

Related Questions