I'm servicing a bank and I'm having trouble making a select .
Structure of table descontos_taxas
:
id | value | customer | data_created
My Scenario: This table stores both fees and discounts in the same table. The rates are positive values and the discount negative values.
I need to get all the values of all the clients, however, if a customer has received a fee and a discount on the same day I need to get only one result with the difference between the two.
So far I've done the following:
SELECT
*
FROM
descontos_taxas as dt
WHERE
YEAR(dt.data_criado) = 2014 and MONTH(dt.data_criado) = 04 and dt.valor < 0
GROUP BY
dt.data_criado, dt.cliente
ORDER BY
data_criado asc;
How to return only the negative values of the result of the sum between rates and discounts applied to the same customer? (Customers without taxes applied on the same day as the discount would add up to 0).
I have this result in select:
valor | cliente | data_criado
-19,90 | Erlon Charles | 2014-04-01
9,90 | Erlon Charles | 2014-04-01
-19,90 | Erlon Charles | 2014-04-05
-19,90 | Erlon Charles | 2014-04-19
I need this result to be equal to this:
valor | cliente | data_criado
-10,00 | Erlon Charles | 2014-04-01 //aqui estão os dados das tuplas 1 e 2 somados
-19,90 | Erlon Charles | 2014-04-05
-19,90 | Erlon Charles | 2014-04-19