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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:04:21+00:00 2026-06-02T16:04:21+00:00

With a ‘standard’ Orders table I am trying to find out how far back

  • 0

With a ‘standard’ Orders table I am trying to find out how far back in time it was that a specific client purchased a certain numbers of items.

Order ID      Items      Client      Date
--------      -----      ------      ----

1             1          Fred        26/04/2012
2             3          John        25/04/2012
3             2          Fred        20/04/2012
4             5          Fred        18/04/2012
5             3          Fred        14/04/2012
6             4          Fred        10/04/2012

So I want to know the time frame covered by the last 10 items purchased by Fred, starting from now and working back.

In this instance I would be trying to identify that Order IDs 1, 3, 4 & 5 added together take me to (or just past) my target total of 10 items, so the date I am looking for is 14/04/2012.

Is there a simple solution to 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-06-02T16:04:22+00:00Added an answer on June 2, 2026 at 4:04 pm

    Off the top of my head…

    CREATE FUNCTION time_of_last(itemcount INT, custid VARCHAR(50))
    RETURNS DATE
    BEGIN
       DECLARE tally INT DEFAULT 0;
       DECLARE ondate DATE;
       DECLARE curritems IN DEFAULT 0;
       DECLARE cur1 CURSOR FOR SELECT t.items, t.orderdate FROM yourtable t
          WHERE customer=custid ORDER BY orderdate DESC;
       DECLARE CONTINUE HANDLER FOR NOT FOUND SET ondate = NULL;
    
       OPEN cur1;
    
       read_loop: LOOP
           FETCH cur1 INTO curritems, ondate;
           SET tally = tally+curritems;
           IF (tally>=itemcount) THEN
                LEAVE read_loop;
           END IF;
       END LOOP;
    
       CLOSE cur1;
    
       RETURN ondate;
    END;
    
    SELECT time_of_last(10, 'Fred');
    

    NB if you want to start gathering other information such as what orders are in the set, then Matt Fenwick’s solution is cleaner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a RegEx expression that will successfully parse the following
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I've got a string that has curly quotes in it. I'd like to replace
I downloaded PostgreSQL from the official website and ran the .dmg installer. After that
I am doing a simple coin flipping experiment for class that involves flipping a
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

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.