query-7a2efd3b910e12bb2a1a00aca86950e0

rq turtle/ttl

.on TwitterOriginally posted

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX schema: <http://schema.org/>
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/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT ?film ?filmLabel ?filmDescription (COUNT(DISTINCT ?headOfGovernmentLabel) AS ?count) (GROUP_CONCAT(DISTINCT CONCAT(?headOfGovernmentLabel, ", "@en, ?description); separator = "; ") AS ?headsOfGovernment)
WHERE {
   {
  SELECT ?headOfGovernment ?position (MIN(?startTime) AS ?earliestStartTime) WHERE {
    ?headOfGovernment wdt:P31 wd:Q5;
                      p:P39 [
                        ps:P39 ?position;
                        pq:P580 ?startTime
                      ].
    ?position wdt:P279+ wd:Q2285706.
  }
  GROUP BY ?headOfGovernment ?position
}
  ?film wdt:P31/wdt:P279* wd:Q11424;
        wdt:P577 ?publicationDate;
        p:P161 ?headOfGovernmentStatement.
  ?headOfGovernmentStatement ps:P161 ?headOfGovernment.
  OPTIONAL { ?headOfGovernmentStatement pq:P453 ?character. ?character rdfs:label ?characterLabel. FILTER(LANG(?characterLabel) = "en") } # label service doesn’t work for this one for some reason – perhaps the query optimizer is being stupid and moving the label service before this optional block, so that ?character isn’t bound yet when the label service runs
  FILTER(?earliestStartTime > ?publicationDate) # *future* head of government
  FILTER NOT EXISTS {
    ?headOfGovernment p:P39 [
      ps:P39 ?otherPosition;
      pq:P580 ?otherStartTime
    ].
    ?otherPosition wdt:P279+ wd:Q2285706.
    FILTER(?otherStartTime < ?publicationDate) # not already a head of government (some people held multiple “head of government” offices, or the same one multiple times)
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
    ?film rdfs:label ?filmLabel;
          schema:description ?filmDescription.
    ?headOfGovernment rdfs:label ?headOfGovernmentLabel.
    ?position rdfs:label ?positionLabel.
  } 
  BIND(IF(BOUND(?characterLabel), CONCAT(?characterLabel, " / "@en, ?positionLabel), ?positionLabel) AS ?description)
}
GROUP BY ?film ?filmLabel ?filmDescription
HAVING(?count > 1)
ORDER BY DESC(?count) ASC(MIN(?publicationDate))

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v18("?_anon_42b5416610844a34809f33a099ae9de149126") v12("?character") v7("?characterLabel") v19("?count") v17("?description"):::projected v10("?earliestStartTime") v10("?film"):::projected v14("?filmDescription"):::projected v13("?filmLabel"):::projected v5("?headOfGovernment") v15("?headOfGovernmentLabel"):::projected v11("?headOfGovernmentStatement") v19("?headsOfGovernment") v4("?otherPosition") v2("?otherStartTime") v8("?position") v16("?positionLabel") v3("?publicationDate") v9("?startTime") a3((" ")) a4((" ")) a2((" ")) a1((" ")) c17(["bd:serviceParam"]):::iri c6(["wd:Q2285706"]):::iri c9(["wd:Q5"]):::iri c19(["#91;AUTO_LANGUAGE#93;,en"]):::literal c10(["wd:Q11424"]):::iri f0[["?count > '1^^xsd:integer'"]] f0 --> v19 f1[["not "]] subgraph f1e0["Exists Clause"] e0f0[["?otherStartTime < ?publicationDate"]] e0f0 --> e0v1 e0f0 --> e0v2 e0a1 --"p:statement/P39"--> e0v3 e0a1 --"p:qualifier/P580"--> e0v1 e0v4 --"p:P39"--> e0a1 e0v3 --"p:direct/P279"--> e0c5 e0v4("?headOfGovernment"):::projected e0v3("?otherPosition"):::projected e0v1("?otherStartTime"):::projected e0v2("?publicationDate"):::projected e0a1((" ")):::projected e0c5(["wd:Q2285706"]):::iri end f1--EXISTS--> f1e0 f1 --> v2 f1 --> v3 f1 --> a2 f1 --> c2 f1 --> v4 f1 --> c3 f1 --> v5 f1 --> c4 f1 --> c5 f1 --> c6 f2[["?otherStartTime < ?publicationDate"]] f2 --> v2 f2 --> v3 a2 --"p:statement/P39"--> v4 a2 --"p:qualifier/P580"--> v2 v5 --"p:P39"--> a2 v4 --"p:direct/P279"--> c6 f3[["?earliestStartTime > ?publicationDate"]] f3 --> v10 f3 --> v3 v5 --"p:direct/P31"--> c9 a3 --"p:statement/P39"--> v8 a3 --"p:qualifier/P580"--> v9 v5 --"p:P39"--> a3 v8 --"p:direct/P279"--> c6 bind5[/"min(?startTime)"/] v9 --o bind5 bind5 --as--o v10 v10 --"p:direct/P31"--> a4 a4 --"p:direct/P279"--> c10 v10 --"p:direct/P577"--> v3 v10 --"p:P161"--> v11 v11 --"p:statement/P161"--> v5 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v11 -."p:qualifier/P453".-> v12 v12 --"rdfs:label"--> v7 end subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c17 --"wikibase:language"--> c19 v10 --"rdfs:label"--> v13 v10 --"schema:description"--> v14 v5 --"rdfs:label"--> v15 v8 --"rdfs:label"--> v16 end bind6[/"if(bound(?characterLabel),concat(?characterLabel,s / ^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>',?positionLabel),?positionLabel)"/] v7 --o bind6 v16 --o bind6 bind6 --as--o v17 bind10[/"min(?publicationDate)"/] v3 --o bind10 bind10 --as--o v18 bind11[/"count(?headOfGovernmentLabel)"/] v15 --o bind11 bind11 --as--o v19 bind12[/"concat(?headOfGovernmentLabel,s, ^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>',?description)"/] v15 --o bind12 v17 --o bind12 bind12 --as--o v19