Error while doing an SQL query

2

I have a small problem with a query, I am trying to add the amounts that exist in my table detalle_ventas, what I want to do is add the quantities that have the same product_id and the same expiration date.

Example:

id_producto | fecha_vencimiento | Total
============|===================|========
1           | 2018-05-03        | 100
------------|-------------------|---------
1           | 2018-12-05        | 150
------------|-------------------|---------
2           | 2017-07-09        | 30

I made this query:

SELECT producto_id, fecha_vencimiento, SUM(cantidad) as cantidad FROM detalle_ventas WHERE fecha_vencimiento = fecha_vencimiento and producto_id = producto_id

But this one adds up all the quantities that are in my database and does not separate them as in the example.

id_producto | fecha_vencimiento | Total
============|===================|========
2           | 2018-05-03        | 280
    
asked by Jhosselin Giménez 10.04.2017 в 14:52
source

1 answer

3

For the operation you want, your query needs a GROUP grouping:

SELECT producto_id, fecha_vencimiento, SUM(cantidad) as cantidad FROM detalle_ventas GROUP BY producto_id, fecha_vencimiento
    
answered by 10.04.2017 / 14:55
source