SUM expression expression not working

3

I'm trying to make a SUM in a query that performs some calculations. Here's the example:

SELECT
 SUM ( (qtd - qtd_devolucao) * valor AS total)
FROM
 produtos
WHERE
 id_pedido = '47'

I'm having the following error:

  

# 1064 - You have a syntax error in your SQL next to 'AS total'

    
asked by anonymous 28.07.2017 / 13:16

1 answer

5

To give a name to the sum it should be out of parentheses, should be the last thing of the expression:

SELECT SUM((qtd - qtd_devolucao) * valor) AS total
   FROM produtos
   WHERE id_pedido = '47'

See working on SQL Fiddle . Also put it in GitHub for future reference .

    
28.07.2017 / 13:24