-4

I need sum quantity in a different field with alias.

Image

enter image description here

Tirth Patel
  • 5,443
  • 3
  • 27
  • 39

2 Answers2

0

I don't what your two table names are but this should work. You're trying to aggregate on idMaterial after joining on idMaterial.

SELECT A.idMaterial, C.nombre_material, A.sum_qty_entrada, B.sum_qty_salida, (A.sum_qty_entrada - B.sum_qty_salida) total 
FROM (
    SELECT idMaterial, SUM(qty) sum_qty_entrada FROM mesa_entrada
    GROUP BY idMaterial
)
    AS A 
JOIN (
    SELECT idMaterial, SUM(qty) sum_qty_salida FROM mesa_salida
    GROUP BY idMaterial
) AS B ON 
    A.idMaterial = B.idMaterial
JOIN mesa_con_nombre_material AS C ON
    A.idMaterial = C.idMaterial
gr1zzly be4r
  • 2,072
  • 1
  • 18
  • 33
0

i think that you need this

SELECT SUM(hr) FROM
    (
      Select sum(qty) as hr FROM table1 
      UNION ALL
      Select sum(qty) as hr FROM table2 
    )a
Ezequiel García
  • 2,616
  • 19
  • 12