Count column values in SPARQL

105 Views Asked by At

I am writing a query in SPARQL for a knowledge graph. I have a column having Country and names of countries repeating. For example: Japan, India, US, India, India, US, US, Romania etc. and so on.

I need to write a SPARQL query that gives me the count of each country in that column. Example: Japan 1, India 3, US 3 etc. and so on.

SELECT ?countryName COUNT(?CountryName)
WHERE
{
...
}
GROUP BY ?countryName
HAVING (?countryName = "Germany")
1

There are 1 best solutions below

0
On

Try something like this:

SELECT ?countryName COUNT(*) AS ?count
WHERE 
{ ?iri a OBJECT . 
?iri rdfs:label ?countryName . 
} 
GROUP BY ?countryName 

Keep in mind that you will need to replace OBJECT with the entity type, and I have assumed you are using the rdfs:label predicate for the labels.