Manada
Manada

Reputation: 81

Multiple SUM MYSQL

I have a query with multiple sum that create a new column. But I have to add a sum of the result of one.

I put sum (TOTALPIEZAS *2) AS test

SELECT
   Lote_Cliente.cliente,
   Lote_Cliente.numero_lote_cliente,
   Produccion.Longitud,
   Produccion.Anchura,
   Produccion.Espesor,
   COUNT(Lote_Cliente.numero_lote_cliente) AS Pales,
   SUM(Produccion.Kilos) AS Peso,
   SUM(Produccion.Piezas) AS TOTALPIEZAS,
   SUM (TOTALPIEZAS *2) AS test,
   CONCAT((Longitud*10), 'x', (Anchura*10),'x',Espesor) AS FINALTAMAÑO
FROM
   Lote_Cliente
INNER JOIN
   Produccion ON Lote_Cliente.numero_lote_cliente = Produccion.Lote_cliente
WHERE
   (Lote_Cliente.fecha_preparacion BETWEEN '2019-06-20' AND '2019-06-20') 
   AND (Produccion.Producto = 'Pizarra')
GROUP BY
   Lote_Cliente.numero_lote_cliente, FINALTAMAÑO

A column test with the result of TOTELPIEZAS * 2

Upvotes: 1

Views: 68

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133360

You can't use a column alias in select .. because the column alias is not avalaible at this moment
You should calculate the sum for produccion.Piezas*2

COUNT(Lote_Cliente.numero_lote_cliente) AS Pales,
SUM(Produccion.Kilos) AS Peso,
SUM(Produccion.Piezas) AS TOTALPIEZAS,
SUM (Produccion.Piezas*2) AS test,

Upvotes: 1

Related Questions