sql - MySQL MAX from SUM -
this freaking me out! got 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 field, must used filter data, call {type}, 'single' or 'multiple' value.
i want max between sum(of each different {reg}) every {id}. obviously, simple representation, table got 64985928 registers , {date} filtering data.
that be, 1st step sum each {reg}:
+----+------+ | id | sum | +----+------+ | 1 | 300 | | 2 | 300 | | 2 | 400 | +----+------+
that's:
select sum(value) table (date between '2009-01-01' , '2009-03-01') group reg;
and then, max each sum, i'm stucked:
+----+------+ | id | max | +----+------+ | 1 | 300 | | 2 | 400 | +----+------+
i've tried:
select a.id, max(b.sum) table a, (select sum(b.value) table b (b.date between '2009-01-01' , '2009-03-01') , (a.id = b.id) group b.reg);
any idea? ps: sorry mistakes.
ps {edit 0002} gonna copy original queries , data, may helps better.
$query:
select clienteid "cliente", sum(saldo) "suma" etl.creditos (titularidad_tipo 'titular') , (mes_datos between '2008-11-01' , '2009-10-01') group nuc order 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, i'm looking is:
+---------+-------------+ | cliente | max | +---------+-------------+ | 64 | 1380690.74 | | 187 | 1828468.71 | | 325 | 26037422.21 | | 389 | 875519.05 | | 495 | 109850.46 | +---------+-------------+
but running:
select clienteid "cliente", max(suma) (select clienteid, sum(saldo) "suma" etl.creditos (mes_datos between '2009-08-01' , '2009-10-01') , (titularidad_tipo 'titular') group clienteid, nuc) sums group clienteid order clienteid;
results as:
+---------+-------------+ | cliente | suma | +---------+-------------+ | 64 | 336879.21 | | 187 | 1232824.51 | | 325 | 3816173.62 | | 389 | 218423.83 | | 495 | 34105.99 | +---------+-------------+
select id, max(reg_sum) ( select id, sum(value) reg_sum table (date between '2009-01-01' , '2009-03-01') group id, reg ) group id
Comments
Post a Comment