假设我有一个uri
http://dbpedia.org/page/Manmohan_Singh
现在,他的标签为dbpprop:years.
现在,他的标签为dbpprop:years.
当我写一个像这样的查询
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') }
我得到每年的结果在不同的行..因此重复其他列的数据.有没有一种方法可以将它作为一个列表中的列表,就像所有年份中的一个逗号分隔或像这样的smthng?
类似于prop dbpedia-owl:office
解决方法
这类似于
Aggregating results from SPARQL query,但问题实际上有点复杂,因为有多个变量有多个结果. ?name,?office和?birthPlace有同样的问题.
您可以使用group_concat来解决这个问题,但是您也需要使用distinct,以避免在连接字符串中多次重复相同的年份. group by减少了解决方案中的行数,但在每个行中,您都有一组未分组的变量值.例如,因为?年不在组中,你有一组年份的值,你必须对它们做一些事情.你可以,例如,选择(样本(?年)为?aYear)从集合中只抓一个,或者你可以像我们在这里做的那样做,然后选择(group_concat(distinct?year; separator =“,”)) as?years)将不同的值连接成一个字符串.
你需要一个像下面这样的查询,它产生一行:
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