Add the same column by different criteria?

2

I'm trying to create a report using ReportViewer that makes a SUM in the same column by different criteria. My query works fine in PHPMyAdmin but in QueryBuilder the ReportViewer does not work and returns an error.

How to solve this problem?

Here is my query.

SELECT e.id, e.tipoOperacao, e.historico, 
SUM(CASE WHEN e.tipoOperacao = 'E' THEN e.qtdTransacao ELSE 0 END) AS QTDENTRADA,
SUM(CASE WHEN e.tipoOperacao = 'S' THEN e.qtdTransacao ELSE 0 END) AS QTDSAIDA,
e.dtTransacao, e.valorTransacao, e.usuario, e.produto_id, p.id AS Expr1, p.descricao, 
p.valorCusto, p.valorVenda, p.controlarEstoque, p.qtdEntEstoque, p.estoqueMin, 
p.estoqueMax, p.status, p.imagem, p.catproduto_id, p.unidproduto_id
FROM estoque e 
INNER JOIN  produto p ON (e.produto_id = p.id)
WHERE e.dtTransacao BETWEEN @DI AND @DF GROUP BY e.produto_id

Here is the error.

    
asked by anonymous 29.09.2016 / 06:10

1 answer

0

Try using the if

SUM(IF(e.tipoOperacao = 'E', e.qtdTransacao, 0)) AS QTDENTRADA
    
29.09.2016 / 16:50