Sum function in SQL database

Asked

Viewed 67 times

0

Tabela

I need to create a user role (user function) which receives as input parameters a store identifier code and a category identifier code and returns as a result an integer value that informs the amount (sum) of all products offered by the shop belonging to this category.

What I tried was this code here:

BEGIN
    DECLARE resultadoSoma int;
    SELECT SUM(produto.idProduto) AS idProduto INTO resultadoSoma FROM produto
    WHERE produto.idLoja = IdLoja AND produto.idCategoria = IdCategoria;
    RETURN resultadoSoma;
END

I tried to put the sum function to add the product table ID. The bank even accepted this operation, but it is not adding up the correct way (logic error, not syntax). Could you help me?

  • If you want to know the quantity, the correct function will be count() instead of the SUM. The SUM is used to add up the values and it makes no sense to add up the values of the ID field. No select you need to use the IdLojaand the IdCategoria as parameter. CREATE FUNCTION... and the query, what result is receiving and what result is waiting?

No answers

Browser other questions tagged

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