SQL SUM() syntax error

Asked

Viewed 83 times

0

I have the following scenario:

A table of products (tbl_prods) that has among other fields the price of each product the type of products and the date of the transaction (dt_trns). The guys can be typ1, typ3 or typ3.

I want a query that returns the sum of the prices of the products sold to a certain user in the year 2016 and in the month of Dec, and if the product is of type 2 (typ2) must have 40% discount.

That’s what I got but it ain’t working:

SELECT SUM(IF(prodtype='typ2',price*0.6,price)) FROM tbl_prods WHERE userId=123 AND YEAR(dt_trns)=2016 AND MONTH(dt_trns)=12;

Upshot = “You have an error in your SQL syntax; …”

Where am I going wrong?

  • It would not be this "." at the end "...12."?

  • there is no point. It was a paragraph point I will withdraw.

  • You’re running that one query in your program or running in some database manager?

  • I’m at the Workbench

  • found, was missing quotes. YEAR(dt_trns)='2016' AND MONTH(dt_trns)='12'

1 answer

0

Missing a return AS. Correct would be:

SELECT SUM(IF(prodtype='typ2',price*0.6,price)) as soma FROM tbl_prods WHERE userId=123 AND YEAR(dt_trns)=2016 AND MONTH(dt_trns)=12;

Browser other questions tagged

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