How to do a COUNT in SPARQL
sparql
Solution
I haven't tried this, but try adding `GROUP BY ?item` to the end of the query. I think without `GROUP BY` it just counts the total number of rows.
Problem
Given this very simple model: ``` @prefix : <http://example.org/tags#> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :tag rdf:type rdf:Property . :item1 rdf:type owl:Thing ; :tag "a"^^xsd:string . :item2 rdf:type owl:Thing ; :tag "a"^^xsd:string , "b"^^xsd:string . :item3 rdf:type owl:Thing ; :tag "a"^^xsd:string , "b"^^xsd:string , "c"^^xsd:string . ``` I am trying to get a list of the items and the count of tags that each has: ``` item tagCount ===== ======== item1 1 item2 2 item3 3 ``` Here is my query: ``` SELECT ?item (count(?tag) as ?tagcount) WHERE { ?item :tag ?tag } ``` However it is returning: ``` item tagCount ===== ======== 6 ``` From what I have read, this should work. I am using Jena 2.6.4