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

  • Home
  • SEARCH
  • 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 8628821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:39:07+00:00 2026-06-12T08:39:07+00:00

I have an article table that holds the current stock for each article. I

  • 0

I have an article table that holds the current stock for each article. I need to know the last date when new stock has arrived, after running out of stock for that specific article.

The table looks like this.

+-----------+-----------------+-------+
| ArticleID |    StockDate    | Stock |
+-----------+-----------------+-------+
|         1 | 1/1/2012 10:15  |   100 |
|         1 | 2/1/2012 13:39  |   -50 |
|         1 | 2/1/2012 15:17  |   -50 |
|         1 | 4/1/2012 08:05  |   100 |
|         2 | 5/1/2012 09:48  |    50 |
|         1 | 6/1/2012 14:21  |   -25 |
|         1 | 7/1/2012 16:01  |    10 |
|         2 | 8/1/2012 13:42  |   -10 |
|         1 | 9/1/2012 09:56  |   -85 |
|         1 | 13/1/2012 08:12 |   100 |
|         1 | 13/1/2012 10:50 |   -15 |
+-----------+-----------------+-------+

The output should look like this.

+-----------+-----------------+
| ArticleID |    StockDate    |
+-----------+-----------------+
|         2 | 5/1/2012 09:48  |
|         1 | 13/1/2012 08:12 |
+-----------+-----------------+

How did i get this output? ArticleID 1 had a 100 in stock but reached 0 for the first time on 2/1/2012 15:17. Then new stock arrived and it hit 0 again at 9/1/2012 09:56. So the result should shows the first date after the last empty stock grouped by ArticleID. ArticleID 2 didn’t had a 0 point, so the first stock date is shown.

I need a result set that can be joined with other queries. So a Stored Procedure does not work for me.

  • 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-12T08:39:08+00:00Added an answer on June 12, 2026 at 8:39 am
    select ArticleID,stockdate from 
    (
        select t.ArticleID, t.stockdate, ROW_NUMBER() Over (partition by t.articleid order by v.articleid desc, stockdate) rn
        from yourtable t
        left join 
        (
            select ArticleID, MAX(stockdate) as msd from yourtable t1
            cross apply (select sum(stock) as stockrt from yourtable where stockdate<=t1.stockdate and ArticleID=t1.ArticleID) rt
            where stockrt = 0
            group by articleid
        ) v
        on t.ArticleID = v.ArticleID
        and t.stockdate>v.msd
    ) v
    where rn=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an table Article that is in relationship with Steps (OneToMany) and Steps
I have one table, e.g. pricerules, that have stored special prices by article for
I have a table articles that has a column company which has list of
I have a simple cakephp app with table articles that has a cat_id column
Lets say I have a table in MySQL called articles, that has a column
I have Article table: id | type | date ----------------------- 1 | A |
So I have 2 entities: Article Category And I have a table that relates
I have a table for articles with several fields ,each article can have photo/photos
I have a table that has multiple duplicate records as the following: ID Title
I have a table that stores articles, like the example: Article table: ID #CategoryID

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.