query-cb3ae31a799c2624db3b1be28b1d1d72
]reply[21:29, 19 June 2020 (UTC)) talk (Andrew GrayI haven't yet worked out how to add "and tell me who out of ?pres was oldest on ?day", though... The second part then figures out for each of those days, how many people had a) a presidential term which had already finished; b) had not yet died. (the +1 is a fudge to make the numbers line up since my query only counts former presidents, that one counts current presidents as well). The first part finds all the days when the count might change - a President starts, ends, or dies.en:Living presidents of the United StatesThis basically replicates the table at
Use at
- https://query.wikidata.org/sparql
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
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 ?day ?pres ?presLabel ?age WHERE
{
{ SELECT distinct ?day WHERE {
{ ?m wdt:P31 wd:Q5 . ?m p:P39 ?st . ?st ps:P39 wd:Q11696 . ?m wdt:P570 ?day } union
{ ?m wdt:P31 wd:Q5 . ?m p:P39 ?st . ?st ps:P39 wd:Q11696 . ?st pq:P580 ?day } union
{ ?m wdt:P31 wd:Q5 . ?m p:P39 ?st . ?st ps:P39 wd:Q11696 . ?st pq:P582 ?day }
} }
optional {
?pres wdt:P31 wd:Q5 . ?pres p:P39 ?st . ?st ps:P39 wd:Q11696 . ?st pq:P582 ?end .
optional { ?pres wdt:P570 ?died } . filter (COALESCE(?died, NOW()) > ?day ) . filter (?day >= ?end ) .
?pres wdt:P569 ?born . bind((?day - ?born) as ?age)
}
filter not exists {
?pres2 wdt:P31 wd:Q5 . ?pres2 p:P39 ?st2 . ?st2 ps:P39 wd:Q11696 . ?st2 pq:P582 ?end2 . filter (?day >= ?end2 ) .
?pres2 wdt:P569 ?born2 . bind((?day - ?born2) as ?age2) . filter( ?age2 < ?age )
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} order by ?day
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v14("?age"):::projected
v8("?age2")
v13("?born")
v7("?born2")
v1("?day"):::projected
v8("?died")
v9("?end")
v4("?end2")
v10("?m")
v12("?pres"):::projected
v5("?pres2")
v11("?st")
v6("?st2")
c11(["bd:serviceParam"]):::iri
c2(["wd:Q5"]):::iri
c13(["#91;AUTO_LANGUAGE#93;,en"]):::literal
c5(["wd:Q11696"]):::iri
f0[["not "]]
subgraph f0e0["Exists Clause"]
e0f0[["?age2 < ?age"]]
e0f0 --> e0v8
e0f0 --> e0v2
e0f1[["?day >= ?end2"]]
e0f1 --> e0v3
e0f1 --> e0v4
e0v5 --"p:direct/P31"--> e0c2
e0v5 --"p:P39"--> e0v6
e0v6 --"p:statement/P39"--> e0c5
e0v6 --"p:qualifier/P582"--> e0v4
e0v5 --"p:direct/P569"--> e0v7
e0bind2[/"?day - ?born2"/]
e0v3 --o e0bind2
e0v7 --o e0bind2
e0bind2 --as--o e0v8
e0v2("?age"):::projected
e0v8("?age2"):::projected
e0v7("?born2"):::projected
e0v3("?day"):::projected
e0v4("?end2"):::projected
e0v5("?pres2"):::projected
e0v6("?st2"):::projected
e0c2(["wd:Q5"]):::iri
e0c5(["wd:Q11696"]):::iri
end
f0--EXISTS--> f0e0
f0 --> v8
f0 --> v14
f0 --> v1
f0 --> v4
f0 --> v5
f0 --> c1
f0 --> c2
f0 --> c3
f0 --> v6
f0 --> c4
f0 --> c5
f0 --> c6
f0 --> c7
f0 --> v7
f1[["?age2 < ?age"]]
f1 --> v8
f1 --> v14
f2[["?day >= ?end2"]]
f2 --> v1
f2 --> v4
v5 --"p:direct/P31"--> c2
v5 --"p:P39"--> v6
v6 --"p:statement/P39"--> c5
v6 --"p:qualifier/P582"--> v4
v5 --"p:direct/P569"--> v7
bind3[/"?day - ?born2"/]
v1 --o bind3
v7 --o bind3
bind3 --as--o v8
subgraph union0[" Union "]
subgraph union0l[" "]
style union0l fill:#abf,stroke-dasharray: 3 3;
subgraph union1[" Union "]
subgraph union1l[" "]
style union1l fill:#abf,stroke-dasharray: 3 3;
v10 --"p:direct/P31"--> c2
v10 --"p:P39"--> v11
v11 --"p:statement/P39"--> c5
v11 --"p:qualifier/P582"--> v1
end
subgraph union1r[" "]
style union1r fill:#abf,stroke-dasharray: 3 3;
v10 --"p:direct/P31"--> c2
v10 --"p:P39"--> v11
v11 --"p:statement/P39"--> c5
v11 --"p:qualifier/P580"--> v1
end
union1r <== or ==> union1l
end
end
subgraph union0r[" "]
style union0r fill:#abf,stroke-dasharray: 3 3;
v10 --"p:direct/P31"--> c2
v10 --"p:P39"--> v11
v11 --"p:statement/P39"--> c5
v10 --"p:direct/P570"--> v1
end
union0r <== or ==> union0l
end
subgraph optional0["(optional)"]
style optional0 fill:#bbf,stroke-dasharray: 5 5;
v12 -."p:direct/P31".-> c2
v12 --"p:P39"--> v11
v11 --"p:statement/P39"--> c5
v11 --"p:qualifier/P582"--> v9
v12 --"p:direct/P569"--> v13
subgraph optional1["(optional)"]
style optional1 fill:#bbf,stroke-dasharray: 5 5;
v12 -."p:direct/P570".-> v8
end
bind4[/"?day - ?born"/]
v1 --o bind4
v13 --o bind4
bind4 --as--o v14
end
subgraph s1["http://wikiba.se/ontology#label"]
style s1 stroke-width:4px;
c11 --"wikibase:language"--> c13
end