当 SPARQL 中的分组结果为空时,防止 GROUP_CONCAT 返回空字符串
Prevent GROUP_CONCAT from returning empty string when grouped results are null in SPARQL
我有一个 SPARQL 查询 returning 我想要什么,保存一件事。当我使用 GROUP_CONCAT
时,我在结果中收到一个空字符串。当分组的值是 null
时,我希望它只是 return 集中的 null
。您可以在下面的示例中看到我的 ?team
结果 return ""
而不是像 ?end
那样简单地 null
是 returning。在空字符串的情况下,我的 ?person
值实际上是 null
。有没有办法让我也可以从 ?team
到 return 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(?team_temp = "", ?team_temp, ?unboundVariable) AS ?team)
#The above check if ?team_temp = "". If it is not, then there is a team and you use ?team-temp as ?team. Otherwise, if ?team_temp = "", you use some unbound variable as ?team, and this unbound variable 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
我有一个 SPARQL 查询 returning 我想要什么,保存一件事。当我使用 GROUP_CONCAT
时,我在结果中收到一个空字符串。当分组的值是 null
时,我希望它只是 return 集中的 null
。您可以在下面的示例中看到我的 ?team
结果 return ""
而不是像 ?end
那样简单地 null
是 returning。在空字符串的情况下,我的 ?person
值实际上是 null
。有没有办法让我也可以从 ?team
到 return 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(?team_temp = "", ?team_temp, ?unboundVariable) AS ?team)
#The above check if ?team_temp = "". If it is not, then there is a team and you use ?team-temp as ?team. Otherwise, if ?team_temp = "", you use some unbound variable as ?team, and this unbound variable 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