frnsys
frnsys

Reputation: 2454

Grouping SPARQL results for the same property?

I have the following SPARQL query:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

SELECT ?subsidiary ?employees
WHERE {
    <http://dbpedia.org/resource/Google> dbo:subsidiary ?subsidiary;
                                   dbo:numberOfEmployees ?employees .
}

And I receive the following response:

{
  "head": {
    "vars": [ "subsidiary" , "employees" ]
  } ,
  "results": {
    "bindings": [
      {
        "subsidiary": { "type": "uri" , "value": "http://dbpedia.org/resource/YouTube" } ,
        "employees": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "53861" }
      } ,
      {
        "subsidiary": { "type": "uri" , "value": "http://dbpedia.org/resource/Motorola_Mobility" } ,
        "employees": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "53861" }
      } ,
      {
        "subsidiary": { "type": "uri" , "value": "http://dbpedia.org/resource/Picnik" } ,
        "employees": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "53861" }
      }
    ]
  }
}

The number of employees is returned multiple times because their are multiple subsidiaries associated with the queried resource. Is it possible to collapse all these subsidiaries into a list and get something more like:

{
  "head": {
    "vars": [ "subsidiary" , "employees" ]
  } ,
  "results": {
    "bindings": [
      {
        "subsidiary": { "type": "uri" , "value": [
             "http://dbpedia.org/resource/YouTube",
             "http://dbpedia.org/resource/Motorola_Mobility",
             "http://dbpedia.org/resource/Picnik"
          ]
        } ,
        "employees": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "53861" }
      }
    ]
  }
}

Upvotes: 3

Views: 562

Answers (1)

AndyS
AndyS

Reputation: 16630

Not really. There isn't a an array datatype in the results format. You can create this association by processing the results, ORDER BY ?subsidiary ?employees will put the rows adjacent that you want to collapse.

There is GROUP_CONCAT but it produces a string which is the "," separated concat of string in a GROUP. It would been parsing. An engine may have extension aggregate functions.

Upvotes: 2

Related Questions