Valentina
Valentina

Reputation: 47

SPARQL Query for common values

I need an help with a SPARQL query. In my ontology I have the entity item, which design an article, it has a property called "keyWord" used to store the main arguments treated in a article. In the Triple Store there is a triple (article, keyWord, argument) for each argument of the article.

I want to know with a query what are the articles that have the maximum number of triples with the same value of the property keyWord, how can I do?

I've tried something like this, but it's wrong because I want to know only the articles that have the maximum number of common arguments:

SELECT ?item (COUNT(?argument) as ?maxArgument)   
WHERE{   
     ?item keyWord ?argument                                                                
}
ORDER BY DESC(?maxArgument)

Upvotes: 0

Views: 464

Answers (1)

vassil_momtchev
vassil_momtchev

Reputation: 1193

According the SPARQL specification in order to calculate aggregate values for a solution, the solution is first divided into one or more groups, and the aggregate value is calculated for each group.

This query returns the number of keywords for each article:

SELECT ?item (COUNT(?argument) as ?maxArgument)   
WHERE{   
     ?item :keyword ?argument                                                                
}
GROUP BY ?item
ORDER BY DESC(?maxArgument)

This query returns the most frequent keywords and their article count:

SELECT (COUNT(?item) as ?articles) ?argument
WHERE{   
     ?item :keyword ?argument                                                                
}
GROUP BY ?argument
ORDER BY DESC(?articles)

The same applies for all other aggregate functions like MAX or MIN.

Upvotes: 2

Related Questions