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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:17:54+00:00 2026-05-20T11:17:54+00:00

Ok, I’ll do my best to explain the problem here: The scenario is that

  • 0

Ok, I’ll do my best to explain the problem here:

The scenario is that we’re trying to keep track of currently on hand inventory for items that are continuously changing in quantity.

The way it’s set up is that any time an item changes in quantity, a row is written to a MySQL table ‘tracking’ that records the item SKU, the latest quantity, and a timestamp.

For example:

Dec. 4, 2010 --- Item #12 --- 200 
Dec. 5, 2010 --- Item #33 --- 150
Dec. 8, 2010 --- Item #12 --- 50

The problem is that we want to answer the question — how many total items do we have at any point in time?

This is harder than it looks because of the sparse data problem.

Let’s say on Dec. 4, 2010, we had 200 of item #12
Then on Dec. 8, 2010, we had 50 of that same item (item #12)
Meanwhile, on Dec. 5, 2010, we had 150 of item #33

On Dec 4, 5, and 8, how many total items do we have?
The right answer is 200 total on Dec. 4, 350 on Dec. 5, and 200 on Dec. 8. How do we get those numbers? Well, we have to carry forward the quantities of items from one date to the next. Even if we don’t specify how much we have of that item on the next day, we have to assume that we had as much as we did the previous day.

So, Dec. 4, we had 200 of Item #12, and so on Dec. 5, we still have 200 of Item #12, on top of which we now have 150 of Item #33. This yields 350 on Dec. 5. Then on Dec. 8, we still have 150 of Item #33, but just 50 of Item #12, yielding just 200 items.

The query should return these results for the above data:

Dec. 4, 2010: 200 items
Dec. 5, 2010: 350 items
Dec. 8, 2010: 200 items

The tricky issue is that there might be thousands of items… this significantly complicates the MYSQL and PHP operation.

I’m looking for an optimal solution here that makes the best use of MYSQL and PHP to solve this problem. Temp tables could work as could jamming data in a big PHP associative array, but I have a feeling there’s a better way to do this.

  • 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-20T11:17:55+00:00Added an answer on May 20, 2026 at 11:17 am

    Assuming your table is called log and has three fields, item, ts (time stamp), and quantity, and that quantity contains deltas instead of totals (as recommended by Cfreak):

    set @total = 0;
    select ts, c, @total := @total + c from 
    (select ts, sum(quantity) c from log group by ts order by ts) days;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have text I am displaying in SIlverlight that is coming from a CMS
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a JSP page retrieving data and when single or double quotes are
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

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.