Returning rows based on sum of column

irriggipituag

New Member
I'm trying to retrieve a subset of data based on the sum of a column. The goal is a script to release backordered items. Say we have 100 BO products and get 50 in stock. I'd like to update the oldest orders where the sum of order qty < 50. So something like: Sample Schema:\[code\]Orders Table:order_id, order_dateorder_products Table:order_product_id, order_id, product_id, product_status, ordered_quantityselect * from products where products_id=1234 and status=1 and sum(products_qty) < 50; \[/code\]Where sum(products_qty) is the total returned qty, not just for the individual row. Not sure if this is possible with a single query or even a subquery, but thought I would ask the experts here first. I'm trying to avoid returning all the rows and then manually count up till I reach the limit.
 
Back
Top