SPARQL将MAX应用于聚合值

编程入门 行业动态 更新时间:2024-10-27 02:23:25
本文介绍了SPARQL将MAX应用于聚合值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个查询:

prefix : <test.example/> select ?anotherUser (COUNT(?anotherItem) as ?countOfSharedLikedItems) WHERE { values ?user {:ania}. ?anotherUser :likes ?anotherItem. filter (?anotherUser != ?user) filter exists {?user :likes ?anotherItem} } group by ?anotherUser order by desc(?countOfSharedLikedItems)

根据此数据运行:

@prefix : <test.example/> . :alice :likes :beethoven. :alice :likes :verdi. :sofia :likes :beethoven. :sofia :likes :verdi. :sofia :likes :rossini. :sofia :likes :mozart. :ania :likes :verdi. :ania :likes :beethoven. :ania :likes :david. :david :likes :ania. :david :likes :beethoven. :david :likes :verdi. :antonino :likes :verdi. :antonino :likes :mozart. :weirdo :likes :katyperry. :beethoven a :recommendable. :verdi a :recommendable. :rossini a :recommendable. :katyperry a :recommendable. :mozart a :recommendable.

工作正常,我正在进行聚合并将值绑定到名为countOfSharedLikedItems的变量.现在,我想获得该参数的最大值,我尝试了以下方法:

It is working fine, I'm making aggregation and bound the value to a variable called countOfSharedLikedItems. Now I want to have the maximum value of that parameter, I tried this:

prefix : <test.example/> select ?anotherUser (MAX(?countOfSharedLikedItems) as ?max) (COUNT(?anotherItem) as ?countOfSharedLikedItems) WHERE { values ?user { :ania }. ?anotherUser :likes ?anotherItem. filter (?anotherUser != ?user) filter exists { ?user :likes ?anotherItem } } group by ?anotherUser order by desc(?countOfSharedLikedItems)

但是变量max的结果始终为空.

But the result for the variable max is always empty.

我怎么了?

提示我确实需要在输出中包含三个变量,分别为anotherUser,countOfSharedLikedItems和max.否则,我只需要做出一个外部选择就可以得到最大值,但是我需要其中的三个,这就是为什么我要问这个原因,因为我自己做不到

Hint I do need to have the three variables in the output which are anotherUser, countOfSharedLikedItems, and max. Otherwise, I would have just make an outer select and I could get the max, but I need the three of them, that's why I am asking because i didn't could do it myself

我尝试过这种 SILLY 解决方案,它可以工作,但显然如此愚蠢

I tried this SILLY solution, it works, but it is obviously so silly

prefix : <test.example/> select ?anotherUser ?countOfSharedLikedItems ?maxSharedLikedItems WHERE { { select ?anotherUser (COUNT(?anotherItem) as ?countOfSharedLikedItems) WHERE { values ?user { :ania }. ?anotherUser :likes ?anotherItem. filter (?anotherUser != ?user) filter exists { ?user :likes ?anotherItem } } group by ?anotherUser order by desc(?countOfSharedLikedItems) } { select (MAX(?countOfSharedLikedItems) as ?maxSharedLikedItems) WHERE { select ?anotherUser (COUNT(?anotherItem) as ?countOfSharedLikedItems) WHERE { values ?user { :ania }. ?anotherUser :likes ?anotherItem. filter (?anotherUser != ?user) filter exists { ?user :likes ?anotherItem } } group by ?anotherUser } } }

请问您是否提出其他更好的解决方案?

could you check please and suggest another better solution?

推荐答案

最外层查询的结构在以下部分具有致命问题:

The structure of your outermost query has a fatal issue in these parts:

select (MAX(?countOfSharedLikedItems) as ?max) (COUNT(?anotherItem) as ?countOfSharedLikedItems) WHERE { ... } group by ?anotherUser

您需要完全了解 group by 的功能. 其中部分中的三元组提供了一堆结果行.例如,在类似

You need to fully understand what group by does. The triples in the where part provide a bunch of result rows. E.g., in a query like

select * where { ?s ?p ?o }

结果是一堆行,每个行都有一个用于每个变量的值.当您添加 group by 子句时,是说您要将这些行划分为一堆集合.例如,如果结果是

the result is a bunch of rows, each of which has a value for each of the variables. When you add a group by clause, you're saying that you want to partition those rows up into a bunch of sets. E.g., if the results had been

s1 p1 o1 s1 p2 o2 s2 p1 o2 s2 p1 o3 s3 p1 o2

然后按?s分组,然后将这些结果划分为以下内容:

and you grouped by ?s, then you partition those results into something like this:

s1 [ p1 o1 ] [ p2 o2 ] s2 [ p1 o2 ] [ p1 o3 ] s3 [ p1 o2 ]

对于每个唯一的s值,您仍然有一堆行,每个行提供一个p值和一个o值.聚合功能对这些束起作用.所以当你做类似的事情

For each unique s value, you still have a bunch of rows, each of which provides a p and an o value. The aggregation functions operate over those bunches. So when you do something like

select ?s (max(?o) as ?oMax) where { ?s ?p ?o } group by ?s

max 聚合可用于每个束,即[o1,o2],[o2,o3]和[o2],并从每个束中产生一个值.像

the max aggregate gets to work on each of the bunches, i.e., on [o1, o2], [o2, o3], and [o2], and produces a single value from each one. So something like

select (count(?o) as ?numO) (max(?numO) as ?oMax) group by ?s

没有道理.当按?s分组时,计数在每个束中进行,并且有一些值,因此您可以对它们进行计数.但是 max 也会尝试在每个束中进行操作,但是束中没有要绑定的?numO绑定.即使您可以使用 count(?o)中的值,每束中只有个,所以 max 返回该值.

doesn't make sense. When you group by ?s, the count operates within each bunch, and there are some values, so you can count them. But max also tries to operate within each bunch, but the bunches don't have any binding of ?numO to look at. And even if you could use the value from count(?o), there'd only be one per bunch, so max would just be returning that value.

更多推荐

SPARQL将MAX应用于聚合值

本文发布于:2023-11-12 09:31:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用于   SPARQL   MAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!