Search code examples
sparqlwikidata

Aggregating over counted variables in SPARQL?


On the Wikidata SPARQL endpoint I am trying to make an histogram of the number of times articles are cited in some particular journal. The citation counts are not explicitly in the databases, so I have to count them first. But then I basically want to histogram to show to citation count distribution, i.e. the number of times a paper is cited X times. So, something like:

SELECT ?work (COUNT(?citing_work) AS ?count)
             (COUNT(?count) AS ?hist)
WHERE {
  ?work wdt:P1433 wd:Q6294930.
  ?citing_work wdt:P2860 ?work.
}
GROUP BY ?work ?count
ORDER BY DESC(?count)

But this does not quite do what I had hoped, and gives zero's for this hist variable:

enter image description here

I did try not outputting the ID of the ?work, but that effectively just removes the first column. But still gives four rows for citation count 17. But I want to have one row with ?count = 17 and ?hist = 4 (see screenshot).

How can I update this SPARQL query?


Solution

  • I think you need two selects. I can do:

    #defaultView:LineChart
    select ?count (count(?work) as ?hist) where {
      {
        SELECT ?work (COUNT(?citing_work) AS ?count)
        WHERE {
          ?work wdt:P1433 wd:Q6294930.   
          ?citing_work wdt:P2860 ?work.
        }
        GROUP BY ?work 
      }
    } 
    group by ?count
    order by ?count