根据这些数据,每个人可以选择性地拥有" smart"谓词,每个部门可能有零个或多个人,我需要找到只包含聪明人的部门。结果应该只包括部门1和2.理想情况下,结果还应包括" smart"每个部门的对象。谢谢!
composer update
答案 0 :(得分:3)
像双重否定之类的东西可能会起作用:
SELECT DISTINCT ?dept WHERE {
?dept p:has ?person .
FILTER NOT EXISTS {
?dept p:has ?person1 .
FILTER NOT EXISTS {
?person1 p:smart ?smartVal
}
}
}
结果:
+---------------+
| dept |
+---------------+
| department:1 |
| department:2 |
+---------------+
使用值:
SELECT ?dept (GROUP_CONCAT(DISTINCT ?smart;separator=";") as ?smartValues) WHERE {
?dept p:has ?person .
?person p:smart ?smart
FILTER NOT EXISTS {
?dept p:has ?person1 .
FILTER NOT EXISTS {
?person1 p:smart ?smartVal
}
}
}
GROUP BY ?dept
结果:
+---------------+-------------+
| dept | smartValues |
+---------------+-------------+
| department:1 | maybe;yes |
| department:2 | maybe |
+---------------+-------------+
答案 1 :(得分:3)
我有一种感觉我之前已经回答了类似的事情,但无论如何有一个相当不错的方法来做到这一点:
select ?dept
(count(?person) as ?pc) (count(?smart) as ?sc)
(group_concat(?smart; separator=',') as ?smarts)
{
?dept p:has ?person .
optional { ?person p:smart ?smart }
}
group by ?dept
having (?pc = ?sc)
即:找到部门,人员和(可用的)智能价值。对于每个部门,找到人数与智能值数量匹配的部门。
-------------------------------------------------------------
| dept | pc | sc | smarts |
=============================================================
| <http://example.com/department#2> | 1 | 1 | "maybe" |
| <http://example.com/department#1> | 2 | 2 | "yes,maybe" |
-------------------------------------------------------------
当您想要获得每个对象的结果时,匹配某些条件group by
/ having
通常是最干净的答案(因为您可以将匹配与过滤分开)。