Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL query and distinct count

Tags:

count

sparql

I have the following query:

SELECT ?tag WHERE {   ?r ns9:taggedWithTag ?tagresource.   ?tagresource ns9:name ?tag } LIMIT 5000 

and the results are:

abc abc abc abc abc abc abc abd ads anb 

I want to get somthing like:

tag | count ----------------- abc     7 abd     1 ads     1 anb     1 

I have tried it with count(*) and count(?tag), but than I get the error message "Variable or "*" expected." Can someone tell me, how to make it right?

like image 355
cupakob Avatar asked Aug 03 '09 17:08

cupakob


1 Answers

If you're using Java and Jena's ARQ, you can use ARQ's extensions for aggregates. Your query would look something like:

SELECT ?tag (count(distinct ?tag) as ?count) WHERE {     ?r ns9:taggedWithTag ?tagresource.     ?tagresource ns9:name ?tag } LIMIT 5000 

The original SPARQL specification from 2008 didn't include aggregates, but the current version, 1.1, from 2013 does.

like image 159
Phil M Avatar answered Oct 10 '22 16:10

Phil M