【问题标题】:SPARQL group by and order by: not orderedSPARQL 分组依据和排序依据:未排序
【发布时间】:2017-11-23 17:26:22
【问题描述】:

我跟进query,其中 schema.org 数据库用于查找类的子级数量 - 作为比我的应用程序更简单的数据库。我想按字母顺序连接孩子的名字。查询:

prefix schema:  <http://schema.org/>
prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>

select    ?child  (group_concat (?string) as ?strings) 
where {
  ?child  rdfs:subClassOf schema:Event .
     ?grandchild rdfs:subClassOf ?child .
  bind (strafter(str(?grandchild), "http://schema.org/") as ?string)
}   group by ?child  order by asc(?string)
limit 20 

给了

schema:PublicationEvent   "OnDemandEvent BroadcastEvent"
schema:UserInteraction    "UserPageVisits UserComments UserPlays UserBlocks UserDownloads UserPlusOnes UserLikes UserCheckins UserTweets"

这不是按字母顺序排列的。如果我将排序顺序替换为desc,结果完全相同。我似乎不明白group byorder by 和可能的bind 是如何交互的。

【问题讨论】:

    标签: sparql jena fuseki


    【解决方案1】:

    需要一个额外的select 子查询来推动组内的订单:

    prefix schema:  <http://schema.org/>
    prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
    
    select    ?child  (group_concat (?string) as ?strings) 
    
    where {
        select * 
        {
         ?child  rdfs:subClassOf schema:Event .
         ?grandchild rdfs:subClassOf ?child .
         bind (strafter(str(?grandchild), "http://schema.org/") as ?string)
        } order by asc(?string)
    }   group by ?child  
    limit 20 
    

    【讨论】:

    • 斯坦尼斯拉夫·克拉林是对的。由于group_concat 适用于多重集,因此无法保证订单会被保留。
    • 我使用jena-fuseki 3.4;在您的示例中,当您将 asc 替换为 desc 时,顺序不会改变吗?
    • 为了让它按照提问者的要求工作,必须翻转选择。 group by 必须是内部的,然后是 order bygroup by 忽略之前的排序
    【解决方案2】:

    18.5.1.7 GroupConcat:

    没有指定字符串的顺序。


    From the horse's mouth:

    Steve Harris 在 2011-04-22 19:01 写道:

    在 2011 年 4 月 22 日 06:18,Jeen Broekstra 写道:

    但是,查看 SPARQL 1.1 查询规范,我认为这不是一个有保证的结果:据我所知,解决方案修饰符 ORDER BY 应该应用于解决方案序列 after 分组和聚合,所以它不会影响GROUP_CONCAT的输入顺序。

    没错。

    【讨论】:

    • group_concat 中字符串的顺序没有指定,而是取自子句的顺序,因此应该由order by 子句定义。后来我发现,需要一个额外的select 子查询。
    • @user855443 这不是真的。斯坦尼斯拉夫·克拉林完全正确。 group_concat 的输入是一个多重集,因此会丢失顺序。根据三重存储,它可能会被保留,但根据 W3C 规范,不能保证它。
    • @StanislavKralin 所以看来我们得等到 SPARQL 1.2 了?
    猜你喜欢
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多