query-11f7ec80c14c31d7ec014ffce38d2542

rq turtle/ttl

Gettinwikiwidit(P1365)replaces (P1366)replaced by (P1366)replaced by

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
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 ?sen ?senLabel ?replaces ?start ?end ?replacedBy WHERE {
  ?sen p:P39 ?stmt.
  ?stmt ps:P39 wd:Q4416090.
  {
    # Get start dates
    ?stmt pq:P580 ?start.
    OPTIONAL { ?stmt pq:P1365 ?replaces. }
    FILTER NOT EXISTS 
    {
      ?sen p:P39 ?stmt2.
      ?stmt2 ps:P39 wd:Q4416090.
      ?stmt2 pq:P582 ?start. # The senator also had another term immediately before this
    }
  }
  UNION
  {
    # Get end dates
    ?stmt pq:P582 ?end.
    OPTIONAL { ?stmt pq:P1366 ?replacedBy. }
    FILTER NOT EXISTS 
    {
      ?sen p:P39 ?stmt2.
      ?stmt2 ps:P39 wd:Q4416090.
      ?stmt2 pq:P580 ?end.  # The senator also had another term immediately after this
    }
  }
  BIND (COALESCE(?start, ?end) AS ?date)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY (?sen) (?date)

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v9("?date") v7("?end"):::projected v8("?replacedBy"):::projected v6("?replaces"):::projected v1("?sen"):::projected v5("?start"):::projected v3("?stmt") v4("?stmt2") c9(["bd:serviceParam"]):::iri c3(["wd:Q4416090"]):::iri c11(["#91;AUTO_LANGUAGE#93;,en"]):::literal v1 --"p:P39"--> v3 v3 --"p:statement/P39"--> c3 subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; f0[["not "]] subgraph f0e0["Exists Clause"] e0v1 --"p:P39"--> e0v2 e0v2 --"p:statement/P39"--> e0c3 e0v2 --"p:qualifier/P580"--> e0v3 e0v3("?end"):::projected e0v1("?sen"):::projected e0v2("?stmt2"):::projected e0c3(["wd:Q4416090"]):::iri end f0--EXISTS--> f0e0 f0 --> v1 f0 --> c1 f0 --> v4 f0 --> c2 f0 --> c3 f0 --> c5 f0 --> v7 v1 --"p:P39"--> v4 v4 --"p:statement/P39"--> c3 v4 --"p:qualifier/P580"--> v7 v3 --"p:qualifier/P582"--> v7 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v3 -."p:qualifier/P1366".-> v8 end end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; f1[["not "]] subgraph f1e1["Exists Clause"] e1v1 --"p:P39"--> e1v2 e1v2 --"p:statement/P39"--> e1c3 e1v2 --"p:qualifier/P582"--> e1v3 e1v1("?sen"):::projected e1v3("?start"):::projected e1v2("?stmt2"):::projected e1c3(["wd:Q4416090"]):::iri end f1--EXISTS--> f1e1 f1 --> v1 f1 --> c1 f1 --> v4 f1 --> c2 f1 --> c3 f1 --> c4 f1 --> v5 v1 --"p:P39"--> v4 v4 --"p:statement/P39"--> c3 v4 --"p:qualifier/P582"--> v5 v3 --"p:qualifier/P580"--> v5 subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v3 -."p:qualifier/P1365".-> v6 end end union0r <== or ==> union0l end bind2[/"?start?end"/] v5 --o bind2 v7 --o bind2 bind2 --as--o v9 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c9 --"wikibase:language"--> c11 end