We help IT Professionals succeed at work.
Get Started

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

Last Modified: 2017-10-27
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.
Watch Question
Database Expert
Awarded 2016
Top Expert 2016
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE