防止分组结果在SPARQL中为null时,防止GROUP_CONCAT返回空字符串

问题描述 投票:2回答:1

我有一个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
1个回答
0
投票

恐怕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
© www.soinside.com 2019 - 2024. All rights reserved.