query-dd99ee752e6cea5d4e8505814c54f210

rq turtle/ttl

hint:SubQuery hint:optimizer "None".

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX psv: <http://www.wikidata.org/prop/statement/value/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT
  ?name ?minister ?moves ?seq ?place_name ?from_lat ?from_lon ?lat ?lon







WHERE {  
   {
  SELECT DISTINCT ?minister ?seq (COALESCE(?from1, ?place) AS ?from) ?place ?place_name ?StartTime WHERE {
     {
  SELECT DISTINCT ?minister (COUNT(DISTINCT(?rs1)) AS ?seq) ?place ?StartTime WHERE {
    INCLUDE %ministers .
    {
      SELECT DISTINCT ?minister (?place AS ?pl1) (?StartTime AS ?st1) (?res_stmt AS ?rs1) WHERE {
        INCLUDE %ministers .
      }
    }
    FILTER ((?st1 < ?StartTime) || ((?st1 = ?StartTime) && (STR (?rs1) <= STR(?res_stmt)))) .
  } GROUP BY ?minister ?place ?StartTime ?res_stmt
  ORDER BY ?minister ?seq
}.
    BIND ((?seq) AS ?seq1) .
    OPTIONAL {
      SELECT ?minister (?seq + 1 AS ?seq1) (?place AS ?from1) WHERE {
          {
  SELECT DISTINCT ?minister (COUNT(DISTINCT(?rs1)) AS ?seq) ?place ?StartTime WHERE {
    INCLUDE %ministers .
    {
      SELECT DISTINCT ?minister (?place AS ?pl1) (?StartTime AS ?st1) (?res_stmt AS ?rs1) WHERE {
        INCLUDE %ministers .
      }
    }
    FILTER ((?st1 < ?StartTime) || ((?st1 = ?StartTime) && (STR (?rs1) <= STR(?res_stmt)))) .
  } GROUP BY ?minister ?place ?StartTime ?res_stmt
  ORDER BY ?minister ?seq
}.
      }
    }
    FILTER (bound(?from1)|| bound(?place)) .
    SERVICE wikibase:label {
       bd:serviceParam wikibase:language "en".
       ?place rdfs:label ?place_name .
    }

  }
}
   {
  SELECT ?minister (MAX(?seq) AS ?moves) WHERE {
     {
  SELECT DISTINCT ?minister (COUNT(DISTINCT(?rs1)) AS ?seq) ?place ?StartTime WHERE {
    INCLUDE %ministers .
    {
      SELECT DISTINCT ?minister (?place AS ?pl1) (?StartTime AS ?st1) (?res_stmt AS ?rs1) WHERE {
        INCLUDE %ministers .
      }
    }
    FILTER ((?st1 < ?StartTime) || ((?st1 = ?StartTime) && (STR (?rs1) <= STR(?res_stmt)))) .
  } GROUP BY ?minister ?place ?StartTime ?res_stmt
  ORDER BY ?minister ?seq
}  } GROUP BY ?minister
}

  hint:SubQuery hint:optimizer "None".

  ?place p:P625 / psv:P625 / wikibase:geoLatitude ?lat .
  ?place p:P625 / psv:P625 / wikibase:geoLongitude ?lon . 
  ?from  p:P625 ?from_coords_stmt .

  ?from_coords_stmt psv:P625 / wikibase:geoLatitude  ?from_lat .
  ?from_coords_stmt psv:P625 / wikibase:geoLongitude ?from_lon . 


  SERVICE wikibase:label {
       bd:serviceParam wikibase:language "en".
       ?minister rdfs:label ?name .
  }
} 
ORDER BY DESC(?moves) ?minister ?seq

Query found at