多个任务可以关联到每个员工(经理)。我想要的输出是(包括单词“in”和“&”):
Employee Name | Assignment
Name 1 | Assignment1 in Location1 & Assignment2 in Location2 &....
Name 2 | Assignment1 in Location2 & Assignment3 in Location1 &....
有没有办法在Sparql
中做到这一点?
这是我到目前为止所拥有的:
select ?name group_concat(DISTINCT ?description; separator("&"))
where
{
?employee :hasName ?name
{
select concat(?name, "In", ?location)
?employee ^:hasManager/:hasAsstName ?name
?employee ^:hasManager/:hasLocation ?location
}
}
这给了我空的员工姓名和大量的?描述。这似乎没有反射(reflect)我的预期。
最佳答案
假设嵌套查询没问题,您应该在那里分配一个变量来对连接进行分组,然后对所有未连接变量的结果进行分组。查询应如下所示:
select ?name (group_concat(DISTINCT ?description; separator = " & ") as ?descriptions)
where
{
?employee :hasName ?name
{
select (concat(?name, " in ", ?location) AS ?description)
?employee ^:hasManager/:hasAsstName ?name
?employee ^:hasManager/:hasLocation ?location
}
}
GROUP BY ?name
注意GROUP_CONCAT
的语法。
如果去掉子查询,速度会快很多。由于我没有您的数据,这里有一个在 DBpedia 上非常相似的查询,不使用子查询:
SELECT ?name (GROUP_CONCAT(DISTINCT ?SpouseInfo; separator = " & ") AS ?SpousesInfo)
{
?name a foaf:Person;
dbo:spouse ?spouse.
?spouse dbo:residence/rdfs:label ?residence;
rdfs:label ?spouse_name
BIND (CONCAT(?spouse_name, " lives in ",?residence) AS ?SpouseInfo)
}
GROUP BY ?name
ORDER BY ?name
LIMIT 100
这是the result 。
关于SPARQL concat 加上多个字段上的 group_concat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113711/