I have a query where I count with a grouping on years. For some of the years there is no items to count and thus no result. I would like to have SPARQL return a count of zero for such years.
I am using the Wikidata Query Service, https://query.wikidata.org, and my present solution is to make a series of values and a union with the actual query. It looks a bit clumsy to me. Is there a better way?
#defaultView:BarChart
select ?year ?number_of_pages ?work_label where {
{
select ?year (sample(?pages) as ?number_of_pages) ?work_label
where {
{
select * where {
values (?year ?pages ?work_label) {
("2000" "0"^^xsd:integer "_")
("2001" "0"^^xsd:integer "_")
("2002" "0"^^xsd:integer "_")
("2003" "0"^^xsd:integer "_")
("2004" "0"^^xsd:integer "_")
("2005" "0"^^xsd:integer "_")
("2006" "0"^^xsd:integer "_")
("2007" "0"^^xsd:integer "_")
("2008" "0"^^xsd:integer "_")
("2009" "0"^^xsd:integer "_")
("2010" "0"^^xsd:integer "_")
("2011" "0"^^xsd:integer "_")
("2012" "0"^^xsd:integer "_")
("2013" "0"^^xsd:integer "_")
("2014" "0"^^xsd:integer "_")
("2015" "0"^^xsd:integer "_")
("2016" "0"^^xsd:integer "_")
}
}
}
union {
?work wdt:P50 wd:Q18921408 .
?work wdt:P1104 ?pages .
?work wdt:P577 ?date .
?work rdfs:label ?long_work_label . filter(lang(?long_work_label) = 'en')
bind(substr(?long_work_label, 1, 20) as ?work_label)
bind(str(year(?date)) as ?year)
}
}
group by ?year ?work ?work_label
order by ?year
}
}