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 4088540
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:59:44+00:00 2026-05-20T18:59:44+00:00

How do you do a query to calculate how much of an item has

  • 0

How do you do a query to calculate how much of an item has been consumed (used up)?

We can find the qty of each item that we purchased in a purchases table with columns Id, ProductId, Qty (decimal), Date

Id, ProductId, Qty, Date    
1,  1,         10,  1/1/11
2,  1,         5,   2/2/11
3,  1,         8,   3/3/11

And how do you then add a count of how many of each row in the purchase table have been consumed – assuming strict FIFO? So in the above example if we know that 14 have been consumed the output would be:

Id, ProductId, Qty, Date,    Consumed    
1,  1,         10,  1/1/11,  10
2,  1,         5,   2/2/11,  4
3,  1,         8,   3/3/11,  0

Hopefully that explains what I mean by an amount consumed query – we know 14 were consumed and that the first purchase was for 10, so all 10 have been consumed. The next purchase was for 5 so we know that 4 of those have been consumed.

Theres two places I can get the consumed data from – the ConsumedItems table: columns Id, ProductId, QtyUsed, Date), or from the ConsumedSummaryView with columns ProductId, QtyUsed (this is the sum of ConsumedItems.QtyUsed)

  • 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-05-20T18:59:44+00:00Added an answer on May 20, 2026 at 6:59 pm

    A little different approach than Richard’s, but I’m not sure which will perform better:

    SELECT
        Purchases.Id,
        Purchases.ProductId,
        Purchases.Qty,
        Purchases.Date, 
        CASE
            WHEN COALESCE (PreviousPurchases.PreviousUsed, 0) + Qty < ConsumedSummaryView.QtyUsed THEN Qty
            ELSE
                CASE
                    WHEN ConsumedSummaryView.QtyUsed - COALESCE (PreviousPurchases.PreviousUsed, 0) < 0 THEN 0 
                    ELSE ConsumedSummaryView.QtyUsed - COALESCE (PreviousPurchases.PreviousUsed, 0)
                END
        END AS Used
    FROM
        Purchases
        INNER JOIN ConsumedSummaryView ON Purchases.ProductId = ConsumedSummaryView.ProductId
        LEFT OUTER JOIN (
            SELECT
                SUM(Purchases_2.Qty) AS PreviousUsed,
                Purchases_1.Id
            FROM
                Purchases AS Purchases_2
                INNER JOIN Purchases AS Purchases_1 ON Purchases_2.Id < Purchases_1.Id
                                                   AND Purchases_2.ProductId = Purchases_1.ProductId
            GROUP BY
                Purchases_1.Id
        ) AS PreviousPurchases ON Purchases.Id = PreviousPurchases.Id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a simple query that checks for how much money a customer has
I wrote the following query to calculate the sum. How can also have this
I recently built a query in SQL that I can use to look at
I have this query to calculate the most common days of the week and
I'm trying to write an HQL query which will calculate an average rating for
I need a MySQL query (or function) to calculate the geographic midpoint of an
I hace sql query through this i want to calculate the records after 05:00:00
In a MySQL query I am using the timediff/time_to_sec functions to calculate the total
I am running a simple DATEDIFF query but it doesn't seem to calculate the
i have a query that in a sub query calculates amount by dividing two

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.