Search code examples
sparqlowl

Aggregating SPARQL Result


I have written a SPARQL query on my own ontology which gives duplication of records, for this I also used group_concat, my query is

SELECT ?Movie (str(?y) as ?Rating) (group_concat (?z;separator=",") as ?Director) 
{ ?Movie :rating ?y;
         :directedBy ?z
 FILTER regex(str(?Movie),'Fantastic_Four') }
  GROUP BY ?y ?Movie

This query gives me the desired result but the output for Director comes in following way:

http://www.semanticweb.org/administrator/ontologies/2017/2/Ontology-Schema_Movie#Josh_Trank,http://www.semanticweb.org/administrator/ontologies/2017/2/Ontology-Schema_Movie#Joshua_Trank

I wonder why this whole URI comes, how to remove this and only print Josh_Trank, Joshua_Trank in my result?


Solution

  • RDF resources are identified by URIs, that's why a list of URIs is concatenated. If you need some human readable name

    1. You could use some string functions resp. REGEX on the URI (see a similar question)
        PREFIX : <http://www.semanticweb.org/administrator/ontologies/2017/2/Ontology-Schema_Movie#>                
        SELECT ?Movie (str(?y) as ?Rating) 
               (group_concat (?director_name;separator=",") as ?Director) {
          ?Movie :rating ?y;
                 :directedBy ?z
          FILTER regex(str(?Movie),'Fantastic_Four')
          BIND(strafter(str(?z),str(:)) as ?director_name)
        }
        GROUP BY ?y ?Movie
    
    1. I really suggest to use the rdfs:label (or something similar) if exists for the human readable name of a URI