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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:31:10+00:00 2026-06-01T15:31:10+00:00

I use SQL at work to gather info, but nothing too advanced (yet). I

  • 0

I use SQL at work to gather info, but nothing too advanced (yet).

I need to determine patterns of inventory instock and out of stock.

I have a table that shows item, location, inventory and date. I want to see if there is a pattern to the times when an item/location is out of stock and instock over time. The file contains a rolling three weeks of data. If an item is consistently going instock or out of stock in three weeks, then I need to know about it to research further.

An item/location is instock if inventory greater than zero. An item/location is out of stock if inventory is zero or negative.

Thanks for any assistance.

Sample data

Item, location, inventory, date
1243, 10,       2,         3/12/2012
1243, 10,       0,         3/13/2012
1243, 10,      -2,         3/14/2012
1243, 10,      -2,         3/15/2012
1243, 10,       4,         3/16/2012

Then additional item, location, inventory, date records.

Open to suggestions for output. I just need to see the item/location and how many times it switched between instock and out of stock in the three weeks worth of data.

  • 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-01T15:31:11+00:00Added an answer on June 1, 2026 at 3:31 pm

    Something like this may be what you’re looking for. This will count the number of times that each combination of item and location moves from “In Stock” to “Out of Stock” or vice versa. Note that the first row for each item and location, cannot be counted as a transition because we don’t know the prior status given this logic.

    SELECT item,
           location,
           SUM( CASE WHEN status = 'In Stock' AND prior_status = 'Out of Stock'
                     THEN 1
                     ELSE 0
                 END) moved_to_out_of_stock,
           SUM( CASE WHEN status = 'Out of Stock' AND prior_status = 'In Stock'
                     THEN 1
                     ELSE 0 
                  END) moved_to_in_stock
      FROM (SELECT item, 
                   location,
                   status,
                   lag( status ) over (partition by item, location
                                           order by dt) prior_status
              FROM (SELECT item, 
                           location, 
                           (case when inventory <= 0 
                                 then 'Out of Stock'
                                 else 'In Stock'
                             end) status,
                           dt
                      FROM your_table))
      GROUP BY item, location
    

    This shows that the item went from Out of Stock to In Stock once and from In Stock to Out of Stock once in the sample data you posted.

    SQL> ed
    Wrote file afiedt.buf
    
      1  with your_table as (
      2    select 1243 item, 10 location, 2 inventory, date '2012-03-12' dt from dual union all
      3    select 1243, 10, 0, date '2012-03-13' from dual union all
      4    select 1243, 10, -2, date '2012-03-14' from dual union all
      5    select 1243, 10, -2, date '2012-03-15' from dual union all
      6    select 1243, 10, 4, date '2012-03-16' from dual
      7  )
      8  SELECT item,
      9         location,
     10         SUM( CASE WHEN status = 'In Stock' AND prior_status = 'Out of Stock'
     11                   THEN 1
     12                   ELSE 0
     13               END) moved_to_out_of_stock,
     14         SUM( CASE WHEN status = 'Out of Stock' AND prior_status = 'In Stock'
     15                   THEN 1
     16                   ELSE 0
     17                END) moved_to_in_stock
     18    FROM (SELECT item,
     19                 location,
     20                 status,
     21                 lag( status ) over (partition by item, location
     22                                         order by dt) prior_status
     23            FROM (SELECT item,
     24                         location,
     25                         (case when inventory <= 0
     26                               then 'Out of Stock'
     27                               else 'In Stock'
     28                           end) status,
     29                         dt
     30                    FROM your_table))
     31*   GROUP BY item, location
    SQL> /
    
          ITEM   LOCATION MOVED_TO_OUT_OF_STOCK MOVED_TO_IN_STOCK
    ---------- ---------- --------------------- -----------------
          1243         10                     1                 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I work for a ISV. Our product can use both SQL Server and Oracle
I rarely use SQL and have a unique issue I need to solve. I
The current convention where I work is to use SQL Server schemas like namespaces
So I just recently started learning about how databases work, how to use SQL
Traditionally, when we use SQL string to done some work, we have to close
I use Sql Server compact database, and my linq query work properly in debug
I use SQL Server 2005. I have a simple logging table that my web
I use SQL Server 2008 and have a table with 5 char typed columns.
How use sql like in PyMongo? >>> db.houses.find().count() 11616 >>> db.houses.find({hid:u16999}).count() 1 >>> db.houses.find({hid:u/9/}).count()
I currently use SQL Server to store products in a large catalog web site.

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.