I am using this query to show Saldo column With Invoices and Payments Values. Works Good but if i dont have an invoice but i have a payment, i can see It until i add an invoice. How can i correct It?
select B.razonsocial as Empresa, Facturas ,
IFNULL(pay,0) as Pagos, (Facturas - IFNULL(pay,0)) Saldo
FROM (select TblFacturasCompras.id_proveedor, TblProveedores.razonsocial,
SUM(case when TblFacturasCompras.tipocomprobante = 'A' or TblFacturasCompras.tipocomprobante='B' or TblFacturasCompras.tipocomprobante = 'NDA' or TblFacturasCompras.tipocomprobante = 'NDB' or TblFacturasCompras.tipocomprobante = 'C' or TblFacturasCompras.tipocomprobante = 'NDC' or TblFacturasCompras.tipocomprobante = 'X' then TblFacturasCompras.total else -TblFacturasCompras.total end) as Facturas
from TblFacturasCompras left join TblProveedores on TblFacturasCompras.id_proveedor = TblProveedores.id group by TblProveedores.id) A
LEFT JOIN (select TblProveedores.id, TblProveedores.razonsocial, SUM(TblRecibosCompras.total) as Pay from
TblProveedores left join TblRecibosCompras on TblRecibosCompras.id_proveedor = TblProveedores.id group by TblProveedores.id) B ON A.id_proveedor = B.id
where B.razonsocial <> '' order by B.razonsocial Asc
you are using a column involved in left join in a where condition
this work as an inner join try add the condition to the related on clause
and a suggestion you could use a IN clause instead of several OR clause