0

I'm trying to get some data from Wikidata. I've got a simple query which fetches information about universities:

SELECT ?item ?itemLabel ?site WHERE {
  ?item (p:P31/ps:P31/(wdt:P279*)) wd:Q38723;
    wdt:P17 ?country;
    wdt:P856 ?site.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "ru,en". }
}

And another query, which gets list of members of the CIS:

SELECT DISTINCT ?state WHERE {
  ?state wdt:P31/wdt:P279* wd:Q3624078;
         p:P463 ?memberOfStatement.
  ?memberOfStatement a wikibase:BestRank;
                     ps:P463 wd:Q7779
  MINUS { ?memberOfStatement pq:P582 ?endTime. }
  MINUS { ?state wdt:P576|wdt:P582 ?end. }
}

Both work fine. But now I want to combine them to get list of universities which are located in the CIS. I try to do it like shown in the answer to this question:

SELECT ?item ?itemLabel ?site WHERE {
  ?item (p:P31/ps:P31/(wdt:P279*)) wd:Q38723;
    wdt:P17 ?country;
    wdt:P856 ?site.
  FILTER(EXISTS {
    SELECT DISTINCT ?state WHERE {
      {
        ?state (wdt:P31/(wdt:P279*)) wd:Q3624078;
          p:P463 ?memberOfStatement.
        ?memberOfStatement rdf:type wikibase:BestRank;
          ps:P463 wd:Q7779.
        MINUS { ?memberOfStatement pq:P582 ?endTime. }
        MINUS { ?state (wdt:P576|wdt:P582) ?end. }
      }
      FILTER(?country = ?state)
    }
  })
  SERVICE wikibase:label { bd:serviceParam wikibase:language "ru,en". }
}

But, for some reason, I get zero results. What am I doing wrong here?

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
keddad
  • 1,398
  • 3
  • 14
  • 35
  • I had an idea that `?state` could be some kind of set, but `FILTER(?country IN ?state)` doesn't even run. SPARQL is so confusing :c – keddad Nov 29 '20 at 14:35
  • 1
    Why not use a subquery: `SELECT ?item ?itemLabel ?site WHERE { ?item (p:P31/ps:P31/(wdt:P279*)) wd:Q38723; wdt:P17 ?state; wdt:P856 ?site. { SELECT DISTINCT ?state WHERE { ?state (wdt:P31/(wdt:P279*)) wd:Q3624078; p:P463 ?memberOfStatement. ?memberOfStatement rdf:type wikibase:BestRank; ps:P463 wd:Q7779. MINUS { ?memberOfStatement pq:P582 ?endTime. } MINUS { ?state (wdt:P576|wdt:P582) ?end. } hint:SubQuery hint:runOnce true } } SERVICE wikibase:label { bd:serviceParam wikibase:language "ru,en". } }` – UninformedUser Nov 29 '20 at 18:13
  • @UninformedUser I didn't know I could do something like this, but it looks like an elegant solution. Thanks! Can you post it as an answer so I could accept it? – keddad Nov 29 '20 at 18:18
  • 1
    I guess your `FILTER EXISTS` approach also works. Just reuse the variable name: `SELECT ?item ?itemLabel ?site WHERE { ?item (p:P31/ps:P31/(wdt:P279*)) wd:Q38723 ; wdt:P17 ?state; wdt:P856 ?site. FILTER EXISTS { SELECT DISTINCT ?state WHERE { { ?state (wdt:P31/(wdt:P279*)) wd:Q3624078; p:P463 ?memberOfStatement. ?memberOfStatement rdf:type wikibase:BestRank; ps:P463 wd:Q7779. MINUS { ?memberOfStatement pq:P582 ?endTime. } MINUS { ?state (wdt:P576|wdt:P582) ?end. } } } } SERVICE wikibase:label { bd:serviceParam wikibase:language "ru,en". } } ` – UninformedUser Nov 29 '20 at 19:12

0 Answers0