query-91de8a1a135f0c18e24c18e4aaa2c3f1
TODO
Use at
- https://query.wikidata.org/sparql
PREFIX wikibase: <http://wikiba.se/ontology#>
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#>
# population of New York City, including sources
#defaultView:LineChart
SELECT ?time ?population (" " AS ?dummy) ?sources WHERE {
{
SELECT ?time ?population ?source WHERE {
wd:Q60 p:P1082 [
ps:P1082 ?population;
pq:P585 ?time;
prov:wasDerivedFrom ?reference
].
OPTIONAL { ?reference pr:P248 ?statedIn. }
OPTIONAL { ?reference pr:P854 ?referenceUrl. }
BIND(COALESCE(?statedIn, ?referenceUrl) AS ?source)
FILTER(BOUND(?source)) # we don’t want references which have neither “stated in” nor “reference URL”
}
}
{
SELECT (GROUP_CONCAT(DISTINCT ?sourceLabel; separator = "; ") AS ?sources_) WHERE {
{
SELECT ?time ?population ?source WHERE {
wd:Q60 p:P1082 [
ps:P1082 ?population;
pq:P585 ?time;
prov:wasDerivedFrom ?reference
].
OPTIONAL { ?reference pr:P248 ?statedIn. }
OPTIONAL { ?reference pr:P854 ?referenceUrl. }
BIND(COALESCE(?statedIn, ?referenceUrl) AS ?source)
FILTER(BOUND(?source)) # we don’t want references which have neither “stated in” nor “reference URL”
}
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?source rdfs:label ?sourceLabel.
}
}
}
# change “source; source; source” to “source; source; and source”, and “source; source” to “source and source”
BIND(
IF(REGEX(?sources_, "; .*;"),
REPLACE(?sources_, "(.*); (.*)", "$1; and $2"), # the first .* is greedy, so this replaces the last ;
REPLACE(?sources_, "(.*); (.*)", "$1 and $2"))
AS ?sources)
}
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v10("?dummy")
v2("?population"):::projected
v4("?reference")
v6("?referenceUrl")
v7("?source")
v7("?sourceLabel")
v9("?sources"):::projected
v8("?sources_")
v5("?statedIn")
v3("?time"):::projected
a1((" "))
a2((" "))
c9(["bd:serviceParam"]):::iri
c4(["wd:Q60"]):::iri
c11(["#91;AUTO_LANGUAGE#93;,en"]):::literal
f0[["bound(?source)"]]
f0 --> v7
a1 --"p:statement/P1082"--> v2
a1 --"p:qualifier/P585"--> v3
a1 --"prov:wasDerivedFrom"--> v4
c4 --"p:P1082"--> a1
subgraph optional0["(optional)"]
style optional0 fill:#bbf,stroke-dasharray: 5 5;
v4 -."p:reference/P248".-> v5
end
subgraph optional1["(optional)"]
style optional1 fill:#bbf,stroke-dasharray: 5 5;
v4 -."p:reference/P854".-> v6
end
bind1[/"?statedIn?referenceUrl"/]
v5 --o bind1
v6 --o bind1
bind1 --as--o v7
f2[["bound(?source)"]]
f2 --> v7
a2 --"p:statement/P1082"--> v2
a2 --"p:qualifier/P585"--> v3
a2 --"prov:wasDerivedFrom"--> v4
c4 --"p:P1082"--> a2
subgraph optional2["(optional)"]
style optional2 fill:#bbf,stroke-dasharray: 5 5;
v4 -."p:reference/P248".-> v5
end
subgraph optional3["(optional)"]
style optional3 fill:#bbf,stroke-dasharray: 5 5;
v4 -."p:reference/P854".-> v6
end
bind3[/"?statedIn?referenceUrl"/]
v5 --o bind3
v6 --o bind3
bind3 --as--o v7
subgraph s1["http://wikiba.se/ontology#label"]
style s1 stroke-width:4px;
c9 --"wikibase:language"--> c11
v7 --"rdfs:label"--> v7
end
bind5[/"?sourceLabel"/]
v7 --o bind5
bind5 --as--o v8
bind6[/"if(regex(?sources_,'; .*;'),replace(?sources_,'(.*); (.*)','$1; and $2'),replace(?sources_,'(.*); (.*)','$1 and $2'))"/]
v8 --o bind6
bind6 --as--o v9
bind7[/"' '"/]
bind7 --as--o v10