S
S
Sergey Beloventsev2017-09-26 11:57:02
PostgreSQL
Sergey Beloventsev, 2017-09-26 11:57:02

What can be added to the query to get the result?

actually here is the query

SELECT bonus, nlevel(tree) - nlevel('2.383') AS level
FROM "user" 
INNER JOIN "bonus_item" 
ON user_from = "user".id 
AND user_to = 383
WHERE (tree ~ '2.383.*{0,5}') 
AND ("mounth" = '9:2017')
order by nl;

I get the result
bonus   | level 
--------+----
 331.00 |  0
  10.40 |  1
  10.20 |  1
   3.20 |  1
  10.40 |  1
   6.20 |  2
   2.60 |  3
  14.76 |  4
   2.82 |  4
   3.16 |  5

I would like to? what would be summed up by that is, so that it turns out like this
bonus   | level 
--------+----
 331.00 |  0
  34.20 |  1
   6.20 |  2
   2.60 |  3
  17.58 |  4
   3.16 |  5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Kuzmichev, 2017-09-26
@Sergalas

Typically, in such cases, grouping is added to the query and aggregate functions are used. Try like this:

SELECT SUM(bonus), nlevel(tree) - nlevel('2.383') AS level
FROM "user" 
INNER JOIN "bonus_item" 
ON user_from = "user".id 
AND user_to = 383
WHERE (tree ~ '2.383.*{0,5}') 
AND ("mounth" = '9:2017')
GROUP BY (nlevel(tree) - nlevel('2.383'))
order by nl;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question