This question already has an answer here:
- SPARQL full aggregation on a group aggregation 1 answer
I have this query:
prefix : <http://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)
That runs on this data:
@prefix : <http://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.
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 : <http://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)
But the result for the variable max
is always empty.
What wrong did I do?
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
Update 1
I tried this SILLY solution, it works, but it is obviously so silly
prefix : <http://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?