0

Can somebody, please help me write a SPARQL query, which would select all currently living politicians, whose political institutions are from a country within the EU?

Currently I have 3 queries, but I don't know how to combine them

The first one checks for people who are living, whose occupation is politician and/or who are a member of a political party

SELECT DISTINCT ?politician ?politicianLabel

WHERE {

  ?politician wdt:P106 wd:Q82955 .  

  FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}

  OPTIONAL {
    ?politician wdt:P102 ?membership .
  }

  FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }

}

The second one shows all EU countries

SELECT ?country ?countryLabel

WHERE {

    ?country wdt:P463 wd:Q458

   SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
  }

}

And the third one finds parties from a specific country

SELECT ?party

WHERE {

  ?party wdt:P31 wd:Q7278

  ?party wdt:17 #EU Countries#

}

But How can I combine these queries? Can somebody help?

Thank you

Nils Riga
  • 90
  • 2
  • 15
  • ehm, just combine those triple patterns and reuse the variables such that you have them connected? – UninformedUser Dec 08 '19 at 19:30
  • like this: `?politician wdt:P106 wd:Q82955 . ?politician wdt:P102 ?party . ?party wdt:P31 wd:Q7278 . ?party wdt:P17 ?country . ?country wdt:P463 wd:Q458 FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}` – UninformedUser Dec 08 '19 at 19:31
  • Oh.. Ok... Well I tried that `SELECT DISTINCT ?politician WHERE { ?politician wdt:P106 wd:Q82955 . OPTIONAL { ?politician wdt:P102 ?membership . } ?politician wdt:P102 ?party . ?party wdt:P31 wd:Q7278 . ?party wdt:P17 ?country . ?country wdt:P463 wd:Q458 FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o} }` But I get "query timeout reached" Any ideas how to optimize the query so that it could give a result? Thank you a lot ! – Nils Riga Dec 09 '19 at 00:06
  • @AKSW I also tried switching them around to start with the country, but no luck. `?country wdt:P463 wd:Q458. ?party wdt:P31 wd:Q7278 . ?party wdt:P17 ?country . ?politician wdt:P106 wd:Q82955 . ?politician wdt:P102 ?party . FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}` – Nils Riga Dec 09 '19 at 00:32

1 Answers1

1

Thank you @AKSW

This works:

WHERE {
?country wdt:P463 wd:Q458.
?party wdt:P31 wd:Q7278 .
?party wdt:P17 ?country .
?politician wdt:P106 wd:Q82955 . hint:Prior hint:runFirst true.
?politician wdt:P102 ?party .
FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}
}

If you start with the country and add the

hint:Prior hint:runFirst true.

line, it is optimized enough to give result.

Nils Riga
  • 90
  • 2
  • 15
  • yeah, sometimes you have to "help" the query optimizer. Just switching around the triple patterns won't usually help as long as there is some query optimization after parsing. And the order of triple patterns in a basic graph pattern doesn't matter, only once you have OPTIONAL (aka left join) or other stuff like BIND and MINUS etc – UninformedUser Dec 09 '19 at 08:19