Query SQL Agrupando niveis de conta
23/06/2017
0
Preciso de uma Query que faça o agrupamento por níveis de conta. Vou explicar melhor:
Tenho uma tabela com contas financeiras.
Exemplo
Conta 30000 Despesas
30100 Beneficios a associados
30101 Farmacia
30102 Cesta Básica
30103 Material escolar
30200 Gastos com funcionários
30201 Salario
30202 Férias
30203 13 salario
Faço registros numa tabela de movimentos usando essas contas e gostaria de poder agrupar assim:
30000 R$ 300.000,00
30100 R$ 300.000,00
30101 R$ 150.000,00
30102 R$ 80.000,00
30103 R$ 70.000,00
Alguem tem ideia de como posso fazer o agrupamento por substring?
Indo além, gostaria de uma sugestão para selecionar o nível da conta também, ou seja:
Desejo agrupar apenar pelo nível 1, então o resultado seria:
30000 R$ 300.000,00
Se pelo nível 3
30100 R$ 300.000,00
Obrigado, aguardo o help de vcs ai.
Obrigado
Renato Reis
Posts
23/06/2017
Renato Reis
select MID(MOFCONTAB,1,3), sum (mofvalor) as saldo from sccamof where year(mofdata)=2017 and month(mofdata)=1 group by MID(MOFCONTAB,1,3) order by MID(MOFCONTAB,1,3)
Assim Tb
select substring(MOFCONTAB,1,3), sum (mofvalor) as saldo from sccamof where year(mofdata)=2017 and month(mofdata)=1 group by substring(MOFCONTAB,1,3) order by substring(MOFCONTAB,1,3)
Mas ainda não consegui.
24/06/2017
Emerson Nascimento
select MIN(MOFCONTAB) conta, sum (mofvalor) saldo
from sccamof
where year(mofdata) = 2017 and month(mofdata) = 1
group by LEFT(MOFCONTAB,1)
order by 1
para agrupar pelo 3o. nível:
select MIN(MOFCONTAB) conta, sum (mofvalor) saldo
from sccamof
where year(mofdata) = 2017 and month(mofdata) = 1
group by LEFT(MOFCONTAB,3)
order by 1
note que o group by é que faz a mágica.
lembrando que isso só funciona se o seu campo mofdata for do tipo data.
26/06/2017
Renato Reis
Minha query:
with dataModule1.qrContabil do
begin
close;
sql.Clear;
sql.Text:=''select MIN(MOFCONTAB) as CONTA, sum(mofvalor) as SALDO FROM SCCAMOF where MOFDATA between :DATA_INI and :DATA_FIN'';
sql.Add('' group by left(MOFCONTAB,3) order by 1'');
Params.ParamByName(''DATA_INI'').asDateTime := edtInicial.Date;
Params.ParamByName(''DATA_FIN'').asDateTime := edtFinal.Date;
Open;
end;
Erro: Field MOFDATA not Found
Dai acrescentei o MOFDATA
sql.Text:=''select MOFDATA, MIN(MOFCONTAB) as CONTA, sum(mofvalor) as SALDO FROM SCCAMOF where MOFDATA between :DATA_INI and :DATA_FIN'';
sql.Add('' group by MOFDATA, left(MOFCONTAB,3) order by 1'');
Erro: Field MOFCONTAB not Found
Onde pode estar errado? Não entendi!
03/07/2017
Emerson Nascimento
with dataModule1.qrContabil do
begin
close;
sql.Clear;
sql.Add('select MIN(MOFCONTAB) as CONTA, sum(mofvalor) as SALDO FROM SCCAMOF where mofdata between :DATA_INI and :DATA_FIN');
sql.Add('group by left(MOFCONTAB,3) order by 1');
Params.ParamByName('DATA_INI').asDateTime := edtInicial.Date;
Params.ParamByName('DATA_FIN').asDateTime := edtFinal.Date;
Open;
end;
Clique aqui para fazer login e interagir na Comunidade :)