SPARQL - How do you use count?

荒凉一梦 提交于 2019-12-01 21:21:47

问题


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


来源:https://stackoverflow.com/questions/9315151/sparql-how-do-you-use-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!