query-585819b3c32caafd5407d957b47f3f28
BIND COALESCE issueIn the following, how do I BIND(COALESCE(?name2, ?name) as ?name3) so that it works (i.e. ?name3 takes the value of ?name2 if it exists, else takes ?name).
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 rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX pr: <http://www.wikidata.org/prop/reference/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT ?item ?name ?name2 ?name3 ?constituency ?party ?gender ?birth_date ?source WHERE {
?item p:P39 ?mem .
?mem ps:P39 wd:Q18607856 ; pq:P2937 wd:Q45380990 .
OPTIONAL {?item p:P973 ?url .
?url pq:P1810 ?name2 .
}
VALUES ?v {wd:Q7532178 wd:Q45383152}.
FILTER(?item=?v).
FILTER NOT EXISTS { ?mem pq:P582 [] . }
OPTIONAL { ?mem pq:P4100|pq:P102 ?group . }
OPTIONAL { ?mem pq:P768 ?area . }
OPTIONAL { ?item wdt:P21 ?genderItem . }
OPTIONAL { ?item wdt:P569 ?DoB . }
OPTIONAL {
?mem prov:wasDerivedFrom ?ref1 .
OPTIONAL { ?ref1 pr:P854 ?source . }
}
BIND(substr(str(?DoB),1,10) as ?birth_date)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
?item rdfs:label ?name .
?area rdfs:label ?constituency .
?group rdfs:label ?party .
?genderItem rdfs:label ?gender .
}
BIND(COALESCE(?name2,?name) as ?name3)
}
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v9("?DoB")
v7("?area")
v12("?birth_date"):::projected
v14("?constituency"):::projected
v16("?gender"):::projected
v8("?genderItem")
v6("?group")
v2("?item"):::projected
v1("?mem")
v13("?name"):::projected
v5("?name2"):::projected
v17("?name3"):::projected
v15("?party"):::projected
v10("?ref1")
v11("?source"):::projected
v4("?url")
v6("?v")
a1((" "))
c6(["wd:Q45380990"]):::iri
c19(["en"]):::literal
c17(["bd:serviceParam"]):::iri
c4(["wd:Q18607856"]):::iri
f0[["not "]]
subgraph f0e0["Exists Clause"]
e0v1 --"p:qualifier/P582"--> e0a1
e0v1("?mem"):::projected
e0a1((" ")):::projected
end
f0--EXISTS--> f0e0
f0 --> v1
f0 --> c1
f0 --> a1
v1 --"p:qualifier/P582"--> a1
f1[["?item = ?v"]]
f1 --> v2
f1 --> v6
v2 --"p:P39"--> v1
v1 --"p:statement/P39"--> c4
v1 --"p:qualifier/P2937"--> c6
subgraph optional0["(optional)"]
style optional0 fill:#bbf,stroke-dasharray: 5 5;
v2 -."p:P973".-> v4
v4 --"p:qualifier/P1810"--> v5
end
bind2[/VALUES ?v/]
bind2-->v6
bind20(["wd:Q7532178"])
bind20 --> bind2
bind21(["wd:Q45383152"])
bind21 --> bind2
subgraph optional1["(optional)"]
style optional1 fill:#bbf,stroke-dasharray: 5 5;
subgraph union0[" Union "]
subgraph union0l[" "]
style union0l fill:#abf,stroke-dasharray: 3 3;
v1 -."p:qualifier/P102".-> v6
end
subgraph union0r[" "]
style union0r fill:#abf,stroke-dasharray: 3 3;
v1 --"p:qualifier/P4100"--> v6
end
union0r <== or ==> union0l
end
end
subgraph optional2["(optional)"]
style optional2 fill:#bbf,stroke-dasharray: 5 5;
v1 -."p:qualifier/P768".-> v7
end
subgraph optional3["(optional)"]
style optional3 fill:#bbf,stroke-dasharray: 5 5;
v2 -."p:direct/P21".-> v8
end
subgraph optional4["(optional)"]
style optional4 fill:#bbf,stroke-dasharray: 5 5;
v2 -."p:direct/P569".-> v9
end
subgraph optional5["(optional)"]
style optional5 fill:#bbf,stroke-dasharray: 5 5;
v1 -."prov:wasDerivedFrom".-> v10
subgraph optional6["(optional)"]
style optional6 fill:#bbf,stroke-dasharray: 5 5;
v10 -."p:reference/P854".-> v11
end
end
bind3[/"substring(str(?DoB),'1^^xsd:integer','10^^xsd:integer')"/]
v9 --o bind3
bind3 --as--o v12
subgraph s1["http://wikiba.se/ontology#label"]
style s1 stroke-width:4px;
c17 --"wikibase:language"--> c19
v2 --"rdfs:label"--> v13
v7 --"rdfs:label"--> v14
v6 --"rdfs:label"--> v15
v8 --"rdfs:label"--> v16
end
bind4[/"?name2?name"/]
v5 --o bind4
v13 --o bind4
bind4 --as--o v17