sparql group by and order by: not ordered

浪子不回头ぞ 提交于 2020-01-15 10:48:11

问题


I follow up on query where the schema.org database is used to find the number of children of a class - as a simpler database than my application. I want to get the names of the children concatenated in alphabetic order. The query

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 

gives

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

which is not alphabetically ordered. If I replace the sort order to desc the result is exactly the same. I seem not to understand how group by, order by and possibly bind interact.


回答1:


An additional select subquery is required to push the order inside the groups:

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 



回答2:


18.5.1.7 GroupConcat:

The order of the strings is not specified.


From the horse's mouth:

On 2011-04-22, at 19:01, Steve Harris wrote:

On 2011-04-22, at 06:18, Jeen Broekstra wrote:

However, looking at the SPARQL 1.1 query spec, I think this is not a guaranteed result: as far as I can tell the solution modifier ORDER BY should be applied to the solution sequence after grouping and aggregation, so it can not influence the order of the input for the GROUP_CONCAT.

That's correct.



来源:https://stackoverflow.com/questions/46982102/group-concat-and-sorting-in-sparql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!