Summing result values in mysql

I'm servicing a bank and I'm finding it hard to do a select.

Table structure descontos_taxas:
      id | value | client / data_create

My scenario: this table holds both rates and discounts in the same table. Rates are positive values and discounts are negative values.
I need to take all the values from all the customers though if a customer has received a fee and a discount on the same day need to get only one result with the difference between the two.

So far I have 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 fees and discounts applied to the same customer? (customers who do not have fees applied on the same day of the discount the amount would be added 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
Author: Erlon Charles, 2014-05-14

3 answers

I added having to filter after group, and separating rates and discounts:

SELECT
    SUM( IF( valor > 0, valor, 0 ) ) AS taxas,
    SUM( IF( valor < 0, -valor, 0 ) ) AS descontos,
    SUM(valor) AS total,
    cliente,
    data_criado
FROM
    descontos_taxas
WHERE
    YEAR(data_criado) = 2014 and MONTH(data_criado) = 04
GROUP BY
    cliente, data_criado
ORDER BY
    data_criado asc
HAVING
    total < 0;
 5
Author: Bacco, 2014-05-15 13:04:46

If I understood it well it would be the sum of the rates, the sums of the discounts and then the difference of the two, grouped by customer and data_created:

SELECT a.cliente, 
       a.data_criado, 
       a.taxas, 
       a.desconto,  
       (a.taxas - abs(a.desconto)) diferenca
FROM (
SELECT cliente, data_criado, 
    sum(CASE WHEN valor > 0 THEN VALOR ELSE 0 END) taxas,
    sum(CASE WHEN valor < 0 THEN VALOR ELSE 0 END) desconto
FROM descontos_taxas
GROUP BY cliente, data_criado) as a

if you still need to filter:

SELECT a.cliente, 
       a.data_criado, 
       a.taxas, 
       a.desconto,  
       (a.taxas - abs(a.desconto)) diferenca
FROM (
SELECT cliente, data_criado, 
    sum(CASE WHEN valor > 0 THEN VALOR ELSE 0 END) taxas,
    sum(CASE WHEN valor < 0 THEN VALOR ELSE 0 END) desconto
FROM descontos_taxas
GROUP BY cliente, data_criado) as a
WHERE date_format(a.data_criado, '%Y-%m') = '2014-04'

as requested the response

SELECT a.cliente, 
       a.data_criado, 
       (a.taxas - abs(a.desconto)) valor
FROM (
SELECT cliente, data_criado, 
    sum(CASE WHEN valor > 0 THEN VALOR ELSE 0 END) taxas,
    sum(CASE WHEN valor < 0 THEN VALOR ELSE 0 END) desconto
FROM descontos_taxas
GROUP BY cliente, data_criado) as a
 2
Author: , 2014-05-15 01:12:43

You need to group the result by client and day, and you need to remove the condition dt.valor < 0 from the query. It needs to enter as a Having (which is processed after operations). See:

SELECT 
     dt.cliente, dt.data_criado, sum(valor) AS resultado
FROM 
    descontos_taxas as dt
WHERE 
    YEAR(dt.data_criacao) = 2014 and MONTH(dt.data_criacao) = 04 
GROUP BY 
    dt.cliente, DATE_FORMAT(data_criado, "%d/%m/%Y")
ORDER BY 
    data_criado asc
HAVING
    resultado < 0;

I haven't tested the query, it's just a targeting.

 1
Author: Arivan Bastos, 2014-05-15 14:37:48