query-1523651a90a3a3da480dc0fc29ed8bb5

rq turtle/ttl

title: Current majors of municipalities of Iceland (should be 64) SELECT ?municipality ?municipalityLabel ?currentMayor ?currentMayorLabel ?startDate WHERE { # Find all municipalities in Iceland ?municipality wdt:P31 wd:Q955655 . ?municipality wdt:P17 wd:Q189 .

# Exclude municipalities with dissolved date (P576) and qualifiers (P1366) MINUS { ?municipality wdt:P576 [] } MINUS { ?municipality wdt:P1366 [] }

# Get the head of government (mayor) statement and its value ?municipality p:P6 ?statement . ?statement ps:P6 ?currentMayor .

# Get the start date (P580) of the mayor's term ?statement pq:P580 ?startDate .

# Get the label of the current mayor ?currentMayor rdfs:label ?currentMayorLabel . FILTER(LANG(?currentMayorLabel) = "en")

# Get the label of the municipality ?municipality rdfs:label ?municipalityLabel . FILTER(LANG(?municipalityLabel) = "en")

# Filter out mayors that do not have the most recent start time FILTER NOT EXISTS { ?municipality p:P6 ?otherStatement . ?otherStatement ps:P6 ?otherMayor . ?otherStatement pq:P580 ?otherStartDate . FILTER (?otherStartDate > ?startDate) } }

Use at

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 ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
#title: Current majors of municipalities of Iceland (should be 64)
SELECT ?municipality ?municipalityLabel ?currentMayor ?currentMayorLabel ?startDate WHERE {
  # Find all municipalities in Iceland
  ?municipality wdt:P31 wd:Q955655 .
  ?municipality wdt:P17 wd:Q189 .

  # Exclude municipalities with dissolved date (P576) and qualifiers (P1366)
  MINUS { ?municipality wdt:P576 [] }
  MINUS { ?municipality wdt:P1366 [] }

  # Get the head of government (mayor) statement and its value
  ?municipality p:P6 ?statement .
  ?statement ps:P6 ?currentMayor .

  # Get the start date (P580) of the mayor's term
  ?statement pq:P580 ?startDate .

  # Get the label of the current mayor
  ?currentMayor rdfs:label ?currentMayorLabel .
  FILTER(LANG(?currentMayorLabel) = "en")

  # Get the label of the municipality
  ?municipality rdfs:label ?municipalityLabel .
  FILTER(LANG(?municipalityLabel) = "en")

  # Filter out mayors that do not have the most recent start time
  FILTER NOT EXISTS {
    ?municipality p:P6 ?otherStatement .
    ?otherStatement ps:P6 ?otherMayor .
    ?otherStatement pq:P580 ?otherStartDate .
    FILTER (?otherStartDate > ?startDate)
  }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v9("?currentMayor"):::projected v7("?currentMayorLabel"):::projected v3("?municipality"):::projected v6("?municipalityLabel"):::projected v5("?otherMayor") v1("?otherStartDate") v4("?otherStatement") v2("?startDate"):::projected v8("?statement") a1((" ")) a2((" ")) c6(["wd:Q955655"]):::iri c8(["wd:Q189"]):::iri f0[["not "]] subgraph f0e0["Exists Clause"] e0f0[["?otherStartDate > ?startDate"]] e0f0 --> e0v1 e0f0 --> e0v2 e0v3 --"p:P6"--> e0v4 e0v4 --"p:statement/P6"--> e0v5 e0v4 --"p:qualifier/P580"--> e0v1 e0v3("?municipality"):::projected e0v5("?otherMayor"):::projected e0v1("?otherStartDate"):::projected e0v4("?otherStatement"):::projected e0v2("?startDate"):::projected end f0--EXISTS--> f0e0 f0 --> v1 f0 --> v2 f0 --> v3 f0 --> c1 f0 --> v4 f0 --> c2 f0 --> v5 f0 --> c3 f1[["?otherStartDate > ?startDate"]] f1 --> v1 f1 --> v2 v3 --"p:P6"--> v4 v4 --"p:statement/P6"--> v5 v4 --"p:qualifier/P580"--> v1 f2[["?municipalityLabel = 'en'"]] f2 --> v6 f3[["?currentMayorLabel = 'en'"]] f3 --> v7 v3 --"p:direct/P31"--> c6 v3 --"p:direct/P17"--> c8 subgraph minus4["MINUS"] style minus4 stroke-width:6px,fill:pink,stroke:red; v3 --"p:direct/P576"--> a1 end subgraph minus5["MINUS"] style minus5 stroke-width:6px,fill:pink,stroke:red; v3 --"p:direct/P1366"--> a2 end v3 --"p:P6"--> v8 v8 --"p:statement/P6"--> v9 v8 --"p:qualifier/P580"--> v2 v9 --"rdfs:label"--> v7 v3 --"rdfs:label"--> v6