Add the same column by different criteria?

Asked

Viewed 255 times

2

I’m trying to create a report using ReportViewer that makes a SUM in the same column for different criteria. My query works well in Phpmyadmin but in Querybuilder of Reportviewer does not work and returns an error.

How to solve this problem ?

here 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 the error.

inserir a descrição da imagem aqui

  • http://answall.com/a/127134/70

1 answer

0

Try to use the instruction if

SUM(IF(e.tipoOperacao = 'E', e.qtdTransacao, 0)) AS QTDENTRADA

Browser other questions tagged

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