2
I’m trying to make a sum of the result of another operation. Below is the code I’m trying to
select
distinct
(SELECT(SUM(CAST(ROUND(ppre.Valor_Custo, 2) as decimal(18,2))) )) *
(select isnull(sum(case when e.Quantidade < 0 then 0 else e.Quantidade
end),0)from Estoque e
inner join deposito d on d.ID = e.ID_Deposito and d.ID_Empresa in (1,2)
and
d.Ativo = 1
where e.ID_Produto = prod.ID)
as 'Custo total'
FROM Produto prod
inner join Produto_Empresa ppre on ppre.ID_Produto = prod.ID
group by prod.id
This is the result of the consultation:
How do I turn these two results into one?
Note: I tried to add one SUM
but it didn’t work out.
Takes the
group by
– Sorack
as @Sorack said, is grouping, if taking the group will bring a sum only, or uses a CTE:
with dados as ( ...aqui vai seu select ...) select sum('Custo total') from dados
– Ricardo Pontual