A
A
Arseny Sokolov2016-02-27 16:39:11
PostgreSQL
Arseny Sokolov, 2016-02-27 16:39:11

How to correctly do subtraction in SELECT PostgreSQL?

There is such a crm_reffin table :

idplay datetotalsumrefid
one2016-02-27 18:47:29.850391teneighteen
22016-02-27 18:47:43.30942720eighteen
32016-02-27 21:36:19.088732100eighteen

There is also a crm_referals table that contains the same id for the refid column from the crm_reffin table .
In addition to everything, there is also a log table , in which there is a lot of information, but there is also information on the transfer of funds by users.
In order to display information about how much the referrer earned and how much he received payments, the following query is executed:
SELECT playdate, totals as credit, (SELECT sum(debate) * 0.1 FROM log WHERE uid IN (SELECT id FROM users WHERE refid = (SELECT id FROM crm_referals WHERE uid = :uid))) AS sumdebet FROM crm_reffin WHERE refid = (SELECT id FROM crm_referals WHERE uid = :uid);

As a result, I get the following table:
pay datecreditsumdebet
2016-02-27 18:47:29.850391ten8451
2016-02-27 18:47:43.309427208451
2016-02-27 21:36:19.0887321008451

Where 10 and 20 are payout amounts and 8451 is the total accumulated payout amount.
I can’t figure out how to properly subtract from the remainder in one query so that the following table is obtained:
pay datecreditsumdebet
2016-02-27 18:47:29.850391ten8451
2016-02-27 18:47:43.309427208441
2016-02-27 21:36:19.0887321008421

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vovik0134, 2016-03-04
@vovik0134

You need to look towards the analytical functions lag and lead.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question