SPARQL - How do you use count?

2019-02-23 23:41发布

问题:

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

回答1:

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 (COUNT(*) AS ?count)
WHERE
{
  ?a <http://xmlns.com/foaf/0.1/topic_interest> ?s}
} GROUP BY ?s