Good afternoon I have the following query:
select distinct(T.Grupo) 'Grupo', MAX(G.Descricao) 'Grupo', sum(E.QtdCat*S.FactConvEst) 'M2 cativos'
from EncLin as E
INNER JOIN Stock as s
on s.CodProd=e.CodProd
inner join terceiros as t
on t.terceiro=e.Terceiro
inner join grupo as g
on g.Grupo=t.grupo
where
E.QtdCat>0 and E.Estado in ('P','N','C') and E.Arm not in ('03P','05G') and E.TpDoc not in ('ENI','ENS')
Group by T.Grupo
order by 3 desc, 2
I needed to add two more columns, one that returns the value of sum (E.QtdCat S.FactConvEst) when E.State = 'N' and another that returns sum (E.QtdCat < in> S.FactConvEst) when E.State = 'C'.
I think this is done with case ... But I'm not sure why I tried it and it's giving wrong values.