Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL MAX from SUM

Tags:

sql

mysql

max

sum

This is freaking me out! Got the following data:

+----+-----+-------+------------+
| ID | REG | VALUE | DATE       |
+----+-----+-------+------------+
| 1  | 1A  | 100   | 2009-01-01 |
| 1  | 1A  | 100   | 2009-02-01 |
| 1  | 1A  | 100   | 2009-03-01 |
| 2  | 1B  | 100   | 2009-01-01 |
| 2  | 1B  | 100   | 2009-02-01 |
| 2  | 1B  | 100   | 2009-03-01 |
| 2  | 1C  | 100   | 2009-01-01 |
| 2  | 1C  | 100   | 2009-02-01 |
| 2  | 1C  | 200   | 2009-03-01 |
+----+-----+-------+------------+

PS {edit 0001} :: there's an extra field, which also must be used for filter data, call it {TYPE}, an could get 'SINGLE' or 'MULTIPLE' as value.

I want to get the MAX between SUM(of each different {REG}) for every {ID}. Obviously, this is a simple representation, table got up to 64985928 registers and {DATE} is the filtering data.

That will be, 1st step get the SUM for each {REG}:

+----+------+
| ID | SUM  |
+----+------+
| 1  | 300  |
| 2  | 300  |
| 2  | 400  |
+----+------+

That's:

SELECT 
  SUM(value) 
FROM 
  table 
WHERE
  (date BETWEEN '2009-01-01' AND '2009-03-01')
GROUP BY
  reg;

And then, get the MAX from each SUM, which is where I'm stucked:

+----+------+
| ID | MAX  |
+----+------+
| 1  | 300  |
| 2  | 400  |
+----+------+

I've tried:

SELECT
  a.id,
  MAX(b.sum)
FROM
  table a,
  (SELECT 
     SUM(b.value) 
   FROM 
     table b 
   WHERE 
     (b.date BETWEEN '2009-01-01' AND '2009-03-01') AND (a.id = b.id)
   GROUP BY
     b.reg);

Any idea? PS: Sorry for mistakes.

PS {edit 0002} Gonna copy original queries and data, so may it helps better.

$QUERY:

SELECT 
  clienteid AS "CLIENTE",
  SUM(saldo) AS "SUMA" 
FROM
  etl.creditos
WHERE
   (titularidad_tipo LIKE 'TITULAR')
AND
   (mes_datos BETWEEN '2008-11-01' AND '2009-10-01')
GROUP BY
  nuc 
ORDER BY
  clienteid;

Got:

+---------+-------------+
| CLIENTE | SUMA        |
+---------+-------------+
| 64      | 1380690.74  |
| 187     | 1828468.71  |
| 187     | 2828102.80  |
| 325     | 26037422.21 |
| 389     | 875519.05   |
| 495     | 20084.93    |
| 495     | 109850.46   |
+---------+-------------+

Then, what I'm looking for is:

+---------+-------------+
| CLIENTE | MAX         |
+---------+-------------+
| 64      | 1380690.74  |
| 187     | 1828468.71  |
| 325     | 26037422.21 |
| 389     | 875519.05   |
| 495     | 109850.46   |
+---------+-------------+  

But running:

SELECT
    clienteid AS "CLIENTE",
    MAX(suma)
FROM
    (SELECT clienteid, SUM(saldo) AS "suma" FROM etl.creditos
    WHERE (mes_datos BETWEEN '2009-08-01' AND '2009-10-01') AND (titularidad_tipo LIKE 'TITULAR')
    GROUP BY clienteid, nuc) AS sums
GROUP BY
    clienteid
ORDER BY
    clienteid;

Results as:

+---------+-------------+
| CLIENTE | SUMA        |
+---------+-------------+
| 64      | 336879.21   |
| 187     | 1232824.51  |
| 325     | 3816173.62  |
| 389     | 218423.83   |
| 495     | 34105.99    |
+---------+-------------+
like image 373
Wolfchamane Avatar asked Aug 26 '13 09:08

Wolfchamane


2 Answers

SELECT ID, MAX(reg_sum)
FROM
(
   SELECT ID, SUM(value) AS reg_sum FROM table 
   WHERE  (date BETWEEN '2009-01-01' AND '2009-03-01')
   GROUP BY  ID, reg
) a GROUP by ID
like image 73
DB_learner Avatar answered Oct 01 '22 18:10

DB_learner


You can add [ order by SUM(value) DESC limit 1 ] to get the maximum value of the query results.

 SELECT  SUM(value) as maxcount  FROM table WHERE (date BETWEEN '2009 01-01' AND '2009-03-01') GROUP BY reg  order by maxcount desc  limit 1;
like image 26
Ahmad Alhusainy Avatar answered Oct 01 '22 19:10

Ahmad Alhusainy