query-f827404faf8544a2f4678d1bacdb3f6f
SPARQL query
Use at
- https://query.wikidata.org/sparql
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
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/>
SELECT DISTINCT ?item ?id ?name
WHERE {
{
# members can have more than one official ID, so take the highest one
SELECT ?item (MAX(xsd:integer(?parliamentID)) AS ?id) WHERE {
?item p:P39 ?ps ; wdt:P5213 ?parliamentID .
?ps ps:P39 wd:Q17277248 ; pq:P2937 wd:Q61165268 .
FILTER NOT EXISTS { ?ps pq:P582 [] }
?item wdt:P5213 ?parliamentID .
}
GROUP BY ?item
}.
?item p:P5213 ?idstatement .
?idstatement ps:P5213 ?id2 .
# we only want to get the name associated with the latest ID
FILTER (xsd:integer(?id2) = ?id)
OPTIONAL { ?idstatement pq:P1810 ?namedAs }
# Their on-wiki label as a fall-back if no "named as"
OPTIONAL { ?item rdfs:label ?enLabel FILTER(LANG(?enLabel) = "en") }
BIND(COALESCE(?namedAs, ?enLabel) AS ?name)
}
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v1("?enLabel")
v7("?id"):::projected
v2("?id2")
v7("?idstatement")
v5("?item"):::projected
v9("?name"):::projected
v8("?namedAs")
v6("?parliamentID")
v4("?ps")
a1((" "))
c6(["wd:Q17277248"]):::iri
c8(["wd:Q61165268"]):::iri
f0[["http://www.w3.org/2001/XMLSchema#integer(?id2) = ?id"]]
f0 --> v2
f0 --> v7
f1[["not "]]
subgraph f1e0["Exists Clause"]
e0v1 --"p:qualifier/P582"--> e0a1
e0v1("?ps"):::projected
e0a1((" ")):::projected
end
f1--EXISTS--> f1e0
f1 --> v4
f1 --> c2
f1 --> a1
v4 --"p:qualifier/P582"--> a1
v5 --"p:P39"--> v4
v5 --"p:direct/P5213"--> v6
v4 --"p:statement/P39"--> c6
v4 --"p:qualifier/P2937"--> c8
v5 --"p:direct/P5213"--> v6
bind3[/"max(http://www.w3.org/2001/XMLSchema#integer(?parliamentID))"/]
v6 --o bind3
bind3 --as--o v7
v5 --"p:P5213"--> v7
v7 --"p:statement/P5213"--> v2
subgraph optional0["(optional)"]
style optional0 fill:#bbf,stroke-dasharray: 5 5;
v7 -."p:qualifier/P1810".-> v8
end
subgraph optional1["(optional)"]
style optional1 fill:#bbf,stroke-dasharray: 5 5;
v5 -."rdfs:label".-> v1
end
bind4[/"?namedAs?enLabel"/]
v8 --o bind4
v1 --o bind4
bind4 --as--o v9