Thank you so much guys!!!!!!! Highly appreciate the explanation (even more if you are on vacation today) @vladimirs_semikins, @qmacro. I managed to do this task!
Interesting this different approach with aggregations on the query itself, it will saved to me some time if I investigate this before creating my algorithm