query-f47c7ae8343fe5022af2f162c621d7e2
Oravrattas
Use at
- https://query.wikidata.org/sparql
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
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 DISTINCT ?date ?age ?president ?presidentLabel
WHERE
{
{
# Dates where who is oldest living expresident can change
SELECT DISTINCT ?date
WHERE
{
{
{
# persons who is/were president of USA
SELECT ?president ?president_enddate ?president_birthdate ?president_deathdate
WHERE
{
?president wdt:P31 wd:Q5.
?president p:P39 ?president_statement.
?president_statement ps:P39 wd:Q11696.
?president_statement pq:P582 ?president_enddate.
?president wdt:P569 ?president_birthdate.
OPTIONAL { ?president wdt:P570 ?president_deathdate. }
}
} BIND(?president_enddate + "P1D"^^xsd:duration AS ?date)
}
UNION
{
{
# persons who is/were president of USA
SELECT ?president ?president_enddate ?president_birthdate ?president_deathdate
WHERE
{
?president wdt:P31 wd:Q5.
?president p:P39 ?president_statement.
?president_statement ps:P39 wd:Q11696.
?president_statement pq:P582 ?president_enddate.
?president wdt:P569 ?president_birthdate.
OPTIONAL { ?president wdt:P570 ?president_deathdate. }
}
} BIND(?president_deathdate + "P1D"^^xsd:duration AS ?date)
}
}
} OPTIONAL
{
{
# Find the age for all living expresidents for each value of ?date
SELECT ?date ?age ?president
WHERE
{
{
# Dates where who is oldest living expresident can change
SELECT DISTINCT ?date
WHERE
{
{
{
# persons who is/were president of USA
SELECT ?president ?president_enddate ?president_birthdate ?president_deathdate
WHERE
{
?president wdt:P31 wd:Q5.
?president p:P39 ?president_statement.
?president_statement ps:P39 wd:Q11696.
?president_statement pq:P582 ?president_enddate.
?president wdt:P569 ?president_birthdate.
OPTIONAL { ?president wdt:P570 ?president_deathdate. }
}
} BIND(?president_enddate + "P1D"^^xsd:duration AS ?date)
}
UNION
{
{
# persons who is/were president of USA
SELECT ?president ?president_enddate ?president_birthdate ?president_deathdate
WHERE
{
?president wdt:P31 wd:Q5.
?president p:P39 ?president_statement.
?president_statement ps:P39 wd:Q11696.
?president_statement pq:P582 ?president_enddate.
?president wdt:P569 ?president_birthdate.
OPTIONAL { ?president wdt:P570 ?president_deathdate. }
}
} BIND(?president_deathdate + "P1D"^^xsd:duration AS ?date)
}
}
}
{
# persons who is/were president of USA
SELECT ?president ?president_enddate ?president_birthdate ?president_deathdate
WHERE
{
?president wdt:P31 wd:Q5.
?president p:P39 ?president_statement.
?president_statement ps:P39 wd:Q11696.
?president_statement pq:P582 ?president_enddate.
?president wdt:P569 ?president_birthdate.
OPTIONAL { ?president wdt:P570 ?president_deathdate. }
}
} FILTER (?president_enddate < ?date) # Must have become expresident
FILTER IF(BOUND(?president_deathdate), ?president_deathdate >= ?date, true) # Must still be living
BIND (?date - ?president_birthdate AS ?age)
}
}
{
# Find the oldest age for a living exprrsident for each value of ?date
SELECT ?date (MAX(?age) AS ?oldest)
WHERE
{
{
# Find the age for all living expresidents for each value of ?date
SELECT ?date ?age ?president
WHERE
{
INCLUDE %dates
INCLUDE %presidents
FILTER (?president_enddate < ?date) # Must have become expresident
FILTER IF(BOUND(?president_deathdate), ?president_deathdate >= ?date, true) # Must still be living
BIND (?date - ?president_birthdate AS ?age)
}
} }
GROUP BY ?date
} FILTER (?age = ?oldest)
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?date