¿Cómo concatenar una lista de valores en sparql?

Suppose I have a uri http://dbpedia.org/page/Manmohan_Singh now he has a list of years in his tag dbpprop:years.

When I write a query like

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX dcterms: <http://purl.org/dc/terms/>
            PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>PREFIX category: <http://dbpedia.org/resource/Category:>
            PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>PREFIX foaf: <http://xmlns.com/foaf/0.1/>PREFIX dbpprop: <http://dbpedia.org/property/>
            PREFIX dbprop: <http://dbpedia.org/property/>PREFIX grs: <http://www.georss.org/georss/>
            PREFIX category: <http://dbpedia.org/resource/Category:>
            PREFIX owl: <http://www.w3.org/2002/07/owl#>
            PREFIX dbpprop: <http://dbpedia.org/property/>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            SELECT DISTINCT ?x ?name ?abs ?birthDate ?birthplace ?year ?party ?office ?wiki WHERE {
            ?x owl:sameAs? dbpedia:Manmohan_Singh.
            ?x dbpprop:name ?name.
            ?x dbpedia-owl:birthDate ?birthDate.
            ?x dbpedia-owl:birthPlace ?birthplace.
            ?x dbpprop:years ?year.
            ?x dbpprop:party ?party.
            ?x dbpedia-owl:office ?office.
            ?x foaf:isPrimaryTopicOf ?wiki.
            ?x rdfs:comment ?abs.
            FILTER(lang(?abs) = 'en')


            }

I get result of each year in different row .. and hence repeating data for other collumns. Is there a way I can get it as a list in just one collumn like all the years in one collumn comma separated or smthng like that?

Similary for the prop dbpedia-owl:office

preguntado el 27 de noviembre de 13 a las 01:11

That duplicate will show you how you can do what you want to do, but you do need to note that the year isn't the only value for which there are multiple values. There are also various names, birthplaces, and offices. -

¿Alguna vez hiciste algún progreso en esto? -

2 Respuestas

Esto es similar a Agregar resultados de la consulta SPARQL, but the problem is actually a bit more complex, because there are multiple variables that have more than one result. ?name, ?office y ?birthPlace tener el mismo problema

You can work around this using group_concat, but you'll need to use distinct as well, to keep from getting, e.g., the same ?year repeated multiple times in your concatenated string. group by reduces the number of rows that you have in a solution, but in each of those rows, you have a planificadas of values for the variables that you no group by. E.g., since ?year no está en el group by, tienes un planificadas de valores para ?year, and you have to do something with them. You could, e.g., select (sample(?year) as ?aYear) to grab just one from the set, or you could do as we've done here, and select (group_concat(distinct ?year;separator=", ") as ?years) to concatenate the distinct values into a string.

You'll want a query like the following, which produces one row:

SELECT ?x
       (group_concat(distinct ?name;separator="; ") as ?names)
       ?abs
       ?birthDate
       (group_concat(distinct ?birthplace;separator=", ") as ?birthPlaces)
       (group_concat(distinct ?year;separator=", ") as ?years)
       ?party
       (group_concat(distinct ?office;separator=", ") as ?offices)
       ?wiki
WHERE {
  ?x owl:sameAs? dbpedia:Manmohan_Singh.
  ?x dbpprop:name ?name.
  ?x dbpedia-owl:birthDate ?birthDate.
  ?x dbpedia-owl:birthPlace ?birthplace.
  ?x dbpprop:years ?year.
  ?x dbpprop:party ?party.
  ?x dbpedia-owl:office ?office.
  ?x foaf:isPrimaryTopicOf ?wiki.
  ?x rdfs:comment ?abs.
  FILTER(langMatches(lang(?abs),"en"))
}
group by ?x ?abs ?birthDate ?party ?wiki

Resultados SPARQL

contestado el 23 de mayo de 17 a las 13:05

Revisa GROUP_CONCAT but it may be easier to retrieve the data in multiple rows (you can sort to put repeats adjacent to each other) and process in code.

respondido 27 nov., 13:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.