SELECT *

MOREIRA 06/11/2015 12:22:31
#453564
olá amigos.. preciso de ajuda..

tenho duas 03 tabelas

tb_cliente
id
nome

tb_receber
idcliente campo = int
vlrcompra campo = double

tb_pagtos
idcliente campo = int
vlrpago campo = double

preciso somar as compras, ou seja. gerar um débito

se já ouver pagamento realizados somar o valor valor pago e sub do valor compra








conseguir só chegar até aqui..

    Sql = [Ô]SELECT IFNULL(SUM(tb_crdto.dsVRecbr), 0) As TotalRecber, SUM(pg.VPagto) as japago FROM tb_cliente tb_clien INNER JOIN tb_clientecredito tb_crdto INNER JOIN tb_clienterecebto pg ON tb_clien.IdCliente = tb_crdto.IdCliente WHERE tb_crdto.IdCliente =[ô][Ô] & idCliente & [Ô][ô][Ô]


fico grato ajuda...
JABA 06/11/2015 12:52:57
#453568
Resposta escolhida
select tb_cliente.Nome, tb_receber.vlrcompra - (SELECT sum(vlrpago) FROM tb_pagtos where idcliente = tb_receber.idcliente) as debito from tb_receber, tb_cliente where tb_receber.idcliente = tb_cliente.id;


ou

select (vlrcompra - (SELECT sum(vlrpago) FROM tb_pagtos where idcliente = tb_receber.idcliente)) as debito from tb_receber

MOREIRA 06/11/2015 13:57:46
#453571
olá jaba. boa tarde...

fiz assim,

na minha tabela tb_clientecredito,

tenho 03 valores para esse ID

2531,00
243,00
128,50

ele só retorna o primeiro... fiz o agrupamento e nada..


  select tb_cliente.IDCliente, tb_cliente.dsClienteNome, tb_clientecredito.dsVRecbr - (SELECT IfNull(sum(VPagto), 0) FROM tb_clienterecebto where idcliente = tb_clientecredito.idcliente) as debito from tb_clientecredito, tb_cliente where tb_cliente.IDCliente = 1228 Group by tb_cliente.IDCliente 


Apenas soma o valor de 2531,00


<a href=[Ô]http://tinypic.com?ref=2411phv[Ô] target=[Ô]_blank[Ô]><img src=[Ô]http://i65.tinypic.com/2411phv.jpg[Ô] border=[Ô]0[Ô] alt=[Ô]Image and video hosting by TinyPic[Ô]></a>
JABA 06/11/2015 15:01:33
#453576
select tb_cliente.IDCliente, tb_cliente.dsClienteNome, (tb_clientecredito.dsVRecbr - (SELECT IfNull(sum(VPagto), 0) FROM tb_clienterecebto where idcliente = tb_clientecredito.idcliente)) as debito from tb_clientecredito, tb_cliente where tb_cliente.IDCliente = tb_clientecredito.idcliente

MOREIRA 06/11/2015 15:48:08
#453579

nao retorna a soma os 03 valores, traz apenas o primeiro registro da tabela que pertecem ao id


   Set Rst = New ADODB.Recordset
Sql = [Ô]select tb_cliente.IDCliente, tb_cliente.dsClienteNome, tb_clientecredito.dsVRecbr - (SELECT IfNull(sum(VPagto), 0) FROM tb_clienterecebto where idcliente = tb_clientecredito.idcliente) as debito from tb_clientecredito, tb_cliente where tb_cliente.IDCliente =[ô][Ô] & idcliente & [Ô][ô][Ô]
Rst.Open Sql, Cnn, 3
If Rst.RecordCount > 0 Then
LblContaVDevedor.Caption = Format(Rst!debito, [Ô]###,##0.00[Ô])
End If


2531,00 <------
243,00
128,50
------------
2902,5 ---> esse seria o retorno

mas só retorna o valor de 2531,00

JABA 06/11/2015 16:42:46
#453580
Citação:

tb_receber
idcliente campo = int
vlrcompra campo = double



Seguindo o exemplo da postagem, o idcliente para a tabela acima pode se repetir?


MOREIRA 06/11/2015 16:54:06
#453581
sim.. ao registrar os pagamento na tb_clienterecebto, ele subtrai apenas o do primeiro valor, onde tem 03 e deveria ser somado, ai sim. será subtraído ao registrar os pagamento
JABA 06/11/2015 17:40:11
#453584
Veja se é assim:

select tb_cliente.Nome, (SELECT sum(tb_receber.vlrcompra) FROM tb_receber where idcliente = tb_cliente.id) - (SELECT sum(vlrpago) FROM tb_pagtos where idcliente = tb_cliente.id) as debito from tb_cliente;
Tópico encerrado , respostas não são mais permitidas