subtracts with selected values from different tables

-2

I have a problem which I hope can help me, I want to add values of a tabla1 and can subtract them from other values selected from another tabla2 under condition of tabla 2 like=´A%´ adding these values and subtracting them from table 1 with values like= 'B%'

Thank you in advance

    
asked by Jose Gil 04.12.2018 в 16:13
source

2 answers

2

I suggest you do a CTE (Common Table Expression) that is basically a temporary table, only if the data in the tables is not very extensive.

It would be something like that

WITH CTE_Tabla2 AS
(
    SELECT Valor FROM Tabla2 WHERE FILTRO LIKE '%N%'
)
SELECT SUM(T1.Valor)-T2.Valor FROM TABLA1 T1, CTE_Tabla2 T2
WHERE T1.FILTRO LIKE '%A%'
GROUP BY GRUPO

You can find more information on how to use CTE here link

    
answered by 04.12.2018 / 23:26
source
0
Select distinct f9930_ts as fecha, f5450_id_co_docto as centro_operativo,
 sum( f9930_vlr_bruto)as [Total_Bruto]- sum f9930_vlr_bruto,
 sum (f9930_precio_uni) as [Precio_Uni],
 Sum (f9930_vlr_neto) as [Neto],
 sum (f9930_vlr_dscto_linea) as cantidad, sum (f9930_vlr_imp) as impuesto
From t9930_pdv_a_movto_venta, t5450_acum_inventarios
where f5450_id_co_docto like '%001%' and t9930_pdv_a_movto_venta.f9930_id_fecha_factura 
between  '2018-02-02'and '2018-08-31' 
group by  f9930_ts, f5450_id_co_docto
order by  fecha
    
answered by 04.12.2018 в 20:12