我有这个查询:
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 ?smax 聚合可用于每个束,即[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应用于聚合值
发布评论