I want to total the multiplication of two columns

Asked

Viewed 282 times

0

SELECT
ID_PRODUTO AS CODIGO,
BARRAS AS CODIGO_BARRAS,
PRODUTO AS NOME,
ESTOQUE AS QUANTIDADE,
CUSTO,
**(ESTOQUE*CUSTO) AS VALOR_TOTAL -- A TOTALIZAÇÃO É PARA ESSA COLUNA**
FROM PRODUTOS
WHERE ESTOQUE >= 1
ORDER BY PRODUTO ASC
  • 1

    If I have understood SUM(ESTOQUE*CUSTO) provide the desired result, but there is no point you have ID_PRODUTO, BARS and PRODUCT in your selection list.

  • Or study the use of a column calculated in table https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html

  • What is the database manager: Firebird, Mysql or SQL Server?

  • I’m using the Firebird.

1 answer

0

I recommend using the concept of SUBSELECT.

Follow the example:

SELECT CODIGO, CODIGO_BARRAS, NOME, (QUANTIDADE * CUSTO) AS TOTAL
FROM(
     SELECT
     ID_PRODUTO AS CODIGO,
     BARRAS AS CODIGO_BARRAS,
     PRODUTO AS NOME,
     SUM(ESTOQUE) AS QUANTIDADE,
     SUM(CUSTO) AS CUSTO
     FROM PRODUTOS
     WHERE ESTOQUE >= 1
     GROUP BY ID_PRODUTO, BARRAS, PRODUTO) res
GROUP BY CODIGO, CODIGO_BARRAS, NOME

First I realized the sum of the column Stockpile and Cost.

Then I performed the multiplication of the two columns generating the Total.

I recommend the concept ode SUBSELECT because it is simpler to understand and works in multiple managers.

  • Friend I’m sorry I’m too forward in this, rsrs, I tried to run the command up and gave invalid, I do not know if it is the tool I am using (IB manager). Invalid token. Dynamic SQL Error. SQL error code = -104. Invalid Expression in the select list (not contained in either an Aggregate Function or the GROUP BY clause).

  • You copied exactly the same code?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.