clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 921124 fiddles created (11955 in the last week).

DECLARE @TablaFactura TABLE ( Factura NVARCHAR(50), FechaFactura DATE, Total INT ) --INSRTANDO DATOS DE EJEMPLO INSERT INTO @TablaFactura VALUES('F1','01/09/2019',10000),('F2','01/09/2019',300),('F4','03/09/2019',500) --CREMOA LA TABLA PAGOS DECLARE @Pagos TABLE ( Pago NVARCHAR(50), Fecha_Pago DATE, FacturaAfectada NVARCHAR(50), Monto INT ) INSERT INTO @Pagos VALUES('P1','01/09/2019','F1',100),('P2','02/09/2019','F1',2000),('P3','05/09/2019','F2',50) SELECT F.Factura,ISNULL(MAX(F.Total)-(SUM(P.Monto)),MAX(F.Total)) AS SALDO_PENDIENTE FROM @TablaFactura F LEFT JOIN @Pagos P ON P.FacturaAfectada=F.Factura AND P.Fecha_Pago BETWEEN '01-09-2019' AND '03-09-2019' WHERE F.FechaFactura BETWEEN '01-09-2019' AND '03-09-2019' GROUP BY F.Factura
Factura SALDO_PENDIENTE
F1 7900
F2 300
F4 500
Warning: Null value is eliminated by an aggregate or other SET operation.
 hidden batch(es)