I am creating a new query ( query ) to obtain the sum of the amounts of entry and exit of a warehouse. The first thing I have done is formulate a query that allows me to separate the inputs from the outputs by placing a sign -
in front of the amount when it is input, I have done that with a case when
:
select productoid, descripcion, codigodelproveedor, marcacodigo, modelo, codigopropio,
case tipooperacion when 'S' then cantidad when 'I' then cantidad * -1 end as cantidad
from ViewParteDeAlmacenProductosDescriptivo
Now what I need is to do a sum
with the result of case
but I am getting the following error:
Msg 195, Level 15, State 10, Line 2 'sum' is not a recognized built-in function name.
This is the new query:
select productoid, descripcion, codigodelproveedor, marcacodigo, modelo, codigopropio,
sum(case tipooperacion when 'S' then cantidad when 'I' then cantidad * -1 end as cantidad) as cantidad
from ViewParteDeAlmacenProductosDescriptivo
group by productoid, descripcion, codigodelproveedor, marcacodigo, modelo, codigopropio
Any ideas?
Try removing the as quantity inside the sum, I've also changed the name of the external quantity variable so that it doesn't match the one you use inside, just in case:
I think the issue is to place the SUM function inside each case, the as would have no problem:
In this way it will calculate the sum of the quantities according to the case .