Search code examples
sparqlrdf

SPARQL: Count Result Lines


I have a hopefully easy to answer question: They query below returns broader concepts of a particular concept along with a confidence value assigned to it. What I want to do now is to just count how many broader concepts there are (with a confidence above a minimal threshold value). The answer to this should be to just count the resulting lines. However, I currently do not understand how to use the COUNT keyword in SPARQL in this particular case.

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX isa: <http://webisa.webdatacommons.org/concept/>
PREFIX isaont: <http://webisa.webdatacommons.org/ontology#>
select distinct ?hypernym ?minConfidence where
{
GRAPH ?g {
<http://webisa.webdatacommons.org/concept/_car_>  skos:broader ?hypernym .
}
?g isaont:hasConfidence ?minConfidence .
FILTER(?minConfidence > 0.1)
}
ORDER BY DESC(?minConfidence)

I tried the follwoing:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX isa: <http://webisa.webdatacommons.org/concept/>
PREFIX isaont: <http://webisa.webdatacommons.org/ontology#>
select (count(*) as ?count) where
{
GRAPH ?g {
<http://webisa.webdatacommons.org/concept/_car_>  skos:broader ?hypernym .
}
?g isaont:hasConfidence ?minConfidence .
FILTER(?minConfidence > 0.1)
}
ORDER BY DESC(?minConfidence)

Unfortunately, this only returns lines with a 1 in each line. What I actually want is a single number.

You can try the queries online, the public endpoint is: http://webisa.webdatacommons.org/sparql

Thank you for your help!


Solution

  • Apparently Virtuoso does some grouping if there is an ORDER BY present, so leaving it out gives the expected result:

    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
    PREFIX isa: <http://webisa.webdatacommons.org/concept/>
    PREFIX isaont: <http://webisa.webdatacommons.org/ontology#>
    SELECT (COUNT(*) as ?count) WHERE
    {
      GRAPH ?g {
        <http://webisa.webdatacommons.org/concept/_car_>  skos:broader ?hypernym .
      }
      ?g isaont:hasConfidence ?minConfidence .
      FILTER(?minConfidence > 0.1)
    }
    

    You can also use SELECT (COUNT(DISTINCT ?hypernym) AS ?count) to make sure you count only the distinct hypernyms.