Subtraction between two SQL fields

0

hello good morning I have the following problem I need to get the difference between two fields. These fields I have to add the total of all their values and then do the subtraction BUT I THROW ANY RESULT ONCE MADE THE REST

SINCE I AM THANK YOUR RESPONSES

SELECT SUM(ovd.precio_unitario_final) - SUM(pd.suma_pago)  AS 
diferencia
FROM pagos p
INNER JOIN orden_venta as ov on p.id_ov = ov.id_ov
INNER JOIN pagos_detalle as pd on p.id_pago = pd.id_pago
INNER JOIN orden_venta_detalle as ovd on ov.id_ov = ovd.id_ov
GROUP BY ovd.precio_unitario_final , pd.suma_pago
    
asked by Dieguito Weed 03.10.2018 в 15:56
source

2 answers

0

Dear one way to do it is this way, it is now important that even if it seems obvious you make sure that the data type of the columns is int or double type

select SUM(ovd.precio_unitario_final) as 'preciofinal',SUM(pd.suma_pago) as 'sumapago',
preciofinal - sumapago as diferencia 
from pagos p inner join orden_venta as ov on p.id_ov = ov.id_ov inner join 
pagos_detalle as pd on p.id_pago = pd.id_pago inner join orden_venta_detalle as ovd on 
ov.id_ov = ovd.id_ov group by ovd.precio_unitario_final , pd.suma_pago
    
answered by 03.10.2018 в 16:00
0
SELECT (SUM(ovd.precio_unitario_final) - SUM(pd.suma_pago))  
AS 
diferencia
FROM pagos p
INNER JOIN orden_venta as ov on p.id_ov = ov.id_ov
INNER JOIN pagos_detalle as pd on p.id_pago = pd.id_pago
INNER JOIN orden_venta_detalle as ovd on ov.id_ov = 
ovd.id_ov
GROUP BY ovd.precio_unitario_final , pd.suma_pago
    
answered by 03.10.2018 в 16:12