query-6130d8668de8f404d6a8ba09b6542aa8
title:Grouping by range of values (external-id count) SELECT ?group (SUM(?count) as ?count_group) (SAMPLE(?sampl) as ?sample) WITH { SELECT ?type (COUNT(DISTINCT ?item) AS ?count) (SAMPLE(?item) as ?sampl) WHERE { ?item wdt:P8034 ?id . ?item wikibase:identifiers ?ids . BIND((?ids) AS ?type) #not really needed } GROUP BY ?type } as %what WHERE { INCLUDE %what BIND( IF(?type < 6, CONCAT("0",str(?type)), #zero added for sorting by group IF(?type < 16, "06-15", "16+")) as ?group) } GROUP BY ?group ORDER BY ?group
Use at
- https://query.wikidata.org/sparql
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
#title:Grouping by range of values (external-id count)
SELECT ?group (SUM(?count) as ?count_group) (SAMPLE(?sampl) as ?sample)
WHERE
{
{
SELECT ?type (COUNT(DISTINCT ?item) AS ?count) (SAMPLE(?item) as ?sampl)
WHERE
{
?item wdt:P8034 ?id .
?item wikibase:identifiers ?ids .
BIND((?ids) AS ?type) #not really needed
}
GROUP BY ?type
} BIND( IF(?type < 6, CONCAT("0",str(?type)), #zero added for sorting by group
IF(?type < 16, "06-15", "16+"))
as ?group)
}
GROUP BY ?group
ORDER BY ?group
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v6("?count"):::projected
v8("?count_group")
v8("?group"):::projected
v3("?id")
v4("?ids")
v2("?item")
v7("?sampl"):::projected
v9("?sample")
v5("?type")
v2 --"wdt:P8034"--> v3
v2 --"wikibase:identifiers"--> v4
bind0[/"?ids"/]
v4 --o bind0
bind0 --as--o v5
bind3[/"count(?item)"/]
v2 --o bind3
bind3 --as--o v6
bind4[/"sample(?item)"/]
v2 --o bind4
bind4 --as--o v7
bind5[/"if(?type < '6^^xsd:integer',concat('0',str(?type)),if(?type < '16^^xsd:integer','06-15','16+'))"/]
v5 --o bind5
bind5 --as--o v8
bind8[/"sum(?count)"/]
v6 --o bind8
bind8 --as--o v8
bind9[/"sample(?sampl)"/]
v7 --o bind9
bind9 --as--o v9