Retrieve data based on sum of rows group by product - SQL, SSMS

Bhagat S
Bhagat S used Ask the Experts™
on
My data comprises of "Stock Qty" and would like to know what the qty recieved was and the purchased price was based on the latest purchase orders - only up to the qty currently available.
I am able to do this to and created a new calculated column called "Stock Qty New".

So in my attached example, I would now like to filter to show on the rows where the sum of "Stock Qty New" is <= to "Stock Qty" by ProductId.
("Stock Qty" is correctly repeating and shown validating purposes).

So for ProductId AA08510, I would want to see row number 1 only as the first Purchase Order has the 13 items which make the current stock.
For ProductId DZ51855, I would want to see row numbers 4,5 only as both Purchase Orders have 145 items (100 + 45) which make the current stock.
For ProductId FC03201, I would want to see row numbers 6,7 only as both Purchase Orders have 4 items (3 + 1) which make the current stock.



Any suggestions much appreciated and let me know if further information required.
Thanks in advance.
Stock.docx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Database Expert
Awarded 2016
Top Expert 2016
Commented:
I think you just need this. Please try

Select * from yourTable
WHERE  [Stock Qty New]  > 0
Bhagat SBusiness Intelligence Analyst

Author

Commented:
Yes, this works fine.
Many thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial