Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:44:22+00:00 2026-06-12T15:44:22+00:00

I have a list of purchases by date. EG: ItemCode, Purchase Date, Purchase Qty

  • 0

I have a list of purchases by date. EG:

ItemCode, Purchase Date, Purchase Qty

  • XXX, 01 Jan 2012, 10
  • XXX, 10 Jan 2012, 5

For the item I have a corresponding Sales transactions:

Item, Sales Date, Sales Qty

  • XXX, 02 Jan 2012, -5
  • XXX, 09 Jan 2012, -3
  • XXX, 11 JAN 2012, -3

I am looking to get a SQL query (Without a cursor), to get the balance on each purchase order quantity. I.e Run each purchase (First in first out) to 0. (For the purposes of aging inventory )

How can you join the Purchases to the Sales to get this balance remaining each purchased Inventory Lot? Is this possible without a cursor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:44:24+00:00Added an answer on June 12, 2026 at 3:44 pm

    Yes.

    You union the two tables together, and run a running total on the resulting set.

       ;with cte as 
       (
           select itemcode, purchasedate as tdate, purchaseqty as qty from purchases
           union
           select itemcode, salesdate, salesqty from sales
       )
       select 
        t1.*,
        SUM(t2.qty)
       from cte t1
        left join cte t2 
            on t1.tdate>=t2.tdate
            and t1.item = t2.item
       group by t1.item, t1.pdate, t1.qty   
    

    To get the stock remaining at any particular time the same principal applies.

    select p1.*, 
        case when (select SUM(abs(qty)) from sales) > SUM(p2.qty) then 0
        else SUM(p2.qty) - (select SUM(abs(qty)) from sales)  end as stockremaining         
        from purchases p1
        left join purchases p2 on p1.item = p2.item 
        and p2.purchasedate <= p1.purchasedate
    group by p1.purchasedate, p1.item, p1.qty
    

    gives

    1   2012-01-01    10    0
    1   2012-01-10     5    4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given a list of purchase events (customer_id,item) 1-hammer 1-screwdriver 1-nails 2-hammer 2-nails 3-screws 3-screwdriver
I've have a table view that consist of item list that depends on the
I have the following query from p in _context.Products where p.Purchases.Sum(item => item.CCAmount) >
Suppose I have a table of customers and a table of purchases. Each purchase
I have a table of purchase list with fields: ItemName, Quantity, UnitPrice, Amount. Note
Assuming I have a list of financial transactions, I have a need to execute
I have list of input area in the form with id like contact1_title, contact2_title,
I have list of articles on the page in table and i want to
I have list of structure. I want to modify a particular data from the
I have list of textareas. For textareas I use tinyMCE Editor. When I start

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.