I have this query
SELECT ?s WHERE {?a <http://xmlns.com/foaf/0.1/topic_interest> ?s}
which returns
aaa
aaa
aaa
bbb
bbb
ccc
However, I want to display it as
aaa | 3
bbb | 2
ccc | 1
I am using dotnetrdf. This is what i tried
SELECT (COUNT(*) AS ?s) WHERE {?a <http://xmlns.com/foaf/0.1/topic_interest> ?s}
and this just gives me the number of rows there are which is 3080.
Can you tell me how to make it right?
Thanks
This is because COUNT(*)
simply counts result rows for each group
If there is no GROUP BY
clause in your query then there is one implicit group of all results hence you just get the number of rows.
If you add a GROUP BY
to your query like the following example you should get the result you want:
SELECT ?s (COUNT(*) AS ?count)
WHERE
{
?a <http://xmlns.com/foaf/0.1/topic_interest> ?s}
} GROUP BY ?s
For people familiar with relational databases, it's very simple: COUNT()
in SPARQL works in the same way as COUNT()
in SQL.