3

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" }
      }
    ]
  }
}
frnsys
  • 2,404
  • 3
  • 21
  • 25

1 Answers1

2

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.

AndyS
  • 16,345
  • 17
  • 21
  • @frnsys Yes, there's no way to get the value out of the SPARQL result, but you can use `group_concat` to combine the values into something that might be easier to parse afterward. E.g., in [Aggregating results from SPARQL query](http://stackoverflow.com/q/18212697/1281433), there's an example of generating results like "[tag1,tag1,tag2,tag3]". – Joshua Taylor Apr 18 '14 at 17:27