我有一个SPARQL查询,该查询返回我想要的内容,保存一件事。当我使用GROUP_CONCAT
时,我会在结果中收到一个空字符串。我希望当分组的值是null
时,它简单地在返回集中是null
。您可以在下面的示例中看到我的?team
结果返回""
,而不是像返回null
那样简单地返回?end
。在为空字符串的情况下,我的?person
值实际上是null
。有没有办法我也可以让?team
返回null
?
SPARQL查询:
SELECT ?event ?start ?end ?team {
SELECT ?event ?start ?end (GROUP_CONCAT(DISTINCT ?person;SEPARATOR=",") AS ?team) {
?event a cls:Event ;
prop:startDate ?start .
OPTIONAL {
?event prop:endDate ?end .
?event prop:teamMember ?person .
}
FILTER (?start >= "2020-05-25" && ?start < "2020-08-31")
} GROUP BY ?event ?start ?end
} ORDER BY ?start
结果:
| event | start | end | team |
|-------------|------------|------------|--------------------------------------------------------------|
| event:Test1 | 2020-05-27 | | "" |
| event:Test3 | 2020-05-28 | 2020-05-29 | "http://foo.bar/person/smith,http://foo.bar/person/williams" |
| event:Test2 | 2020-05-29 | | "" |
恐怕SPARQL规范(请参阅here)证实了您观察到的正确行为。
要了解为什么会这样,请想象您没有执行GROUP_CONCAT,而是执行了COUNT。然后,如果一个团队没有成员,则希望看到0,而不是null。
为了得到想要的东西,我会在第一次迭代时尝试:
SELECT ?event ?start ?end ?team {
BIND(IF(BOUND(?person), ?team-temp, ?person) AS ?team)
#The above checks if there is a person. If there is, then there is a team and you use ?team-temp as ?team. Otherwise, if ?team-temp = "", which only happens if ?person is not bound, you use ?person as ?team. But ?person is null, so ?team will be null.
SELECT ?event ?start ?end (GROUP_CONCAT(DISTINCT ?person;SEPARATOR=",") AS ?team-temp) {
?event a cls:Event ;
prop:startDate ?start .
OPTIONAL { ?event prop:endDate ?end }
OPTIONAL { ?event prop:teamMember ?person }
#Notice that if we want to match ?end and ?person optionally AND independently, then we need two optional statements above here, instead of one large one.
FILTER (?start >= "2020-05-25" && ?start < "2020-08-31")
} GROUP BY ?event ?start ?end
} ORDER BY ?start