Grouping SPARQL results for the same property?

故事扮演 提交于 2019-12-07 14:37:16

问题


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" }
      }
    ]
  }
}

回答1:


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.



来源:https://stackoverflow.com/questions/23121038/grouping-sparql-results-for-the-same-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!