SELECT "bonus", (nlevel(tree_path) - nlevel('2.383')) as nl,count(nl)
FROM "user"
INNER JOIN "user_bonus_item"
ON user_id_from = "user".id
WHERE ("tree_path" ~ '2.383.*{0,5}')
AND user_id_to = user_id_from
AND ("user_bonus_item"."mounth_bonus" = '9:2017')
AND ("bonus" > 50)
ORDER BY "nl";
bonus | nl
--------+----
305.00 | 0
52.00 | 1
52.00 | 1
51.00 | 1
52.00 | 2
60.00| 3