简体   繁体   中英

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

I'm trying to load all the subtaxons of Biota/Q2382433 (ie 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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM