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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:13:31+00:00 2026-06-08T10:13:31+00:00

I have a database that tracks the location of inventory. The application was poorly

  • 0

I have a database that tracks the location of inventory.

The application was poorly written and is a mess of spaghetti code with no MVC separation and updates to the model can happen in any number of files.

When the application was first written it only needed to track the location the inventory currently resides in so a SiteID column exists in the inventory table that lists it’s currently assigned to.

Since then the owner has decided he wants to maintain a history of which locations the inventory is assigned to. Rather than trawl through all the code to try and find all the places it possibly updates I added a trigger ON UPDATEand ON INSERT to the inventory table that records movements in a history table.

The initial requirement was to be able to view the movement history of a piece of inventory which is easily solved with SELECT * FROM history WHERE InvID = X ORDER BY timestamp DESC

Now I’ve been asked to produce a list for a given location that shows when a piece of inventory is assigned to the site, and the date it leaves (example below). Ideally I’d like to achieve this without modifying the existing schema and triggers if possible.

I’m able to retrieve a list of all the inventory that was assigned to the site at one point and the date which it was assigned. What I’m struggling with is how to find the date that piece of inventory left the site. I could do it within the application but I wondered if it’s possible with SQL.

Inventory table

InventoryID     InventoryName    SiteID
===========     =============    ======
          1     Widget A         $ID
          2     Widget B         $ID
          3     Widget C         $ID

Locations table

SiteID    SiteName
======    ========
     1    Somewhere
     2    Nowhere
     3    Anywhere

History table

InvID    SiteID    Timestamp
=====    ======    =========
    1         1    2012-01-01
    1         2    2012-01-02
    2         1    2012-01-03
    1         1    2012-01-04

New view should look something like this

InvID    DateIn        DateOut
=====    ==========    ==========
    1    2012-01-01    2012-01-02
    2    2012-01-03    NULL
    1    2012-01-04    NULL   

Psuedo code to produce table with a programming language (post-processing SQL query

SELECT * FROM history WHERE SiteID = 1

foreach (row in result) {
    DateOut = SELECT * FROM history WHERE InvID = result.InvID AND timestamp > result.timestamp LIMIT 1
}
  • 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-08T10:13:33+00:00Added an answer on June 8, 2026 at 10:13 am

    try this:

    Its just possible with left outer join to the same table and take the min date from the right table

    select H1.InvID,I.InventoryName,H1.SiteID,H1.[Timestamp] [date in],
    MIN(H2.[Timestamp]) [date out] 
    from    History H1 left outer join History H2
    on      H1.InvID=H2.InvID and H1.[Timestamp]<H2.[Timestamp]
    join Inventory I on I.InventoryID= H1.InvID
    where   H1.SiteID=1
    group by    H1.InvID,I.InventoryName,H1.SiteID,H1.[Timestamp]
    

    Sql Fiddle demo

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

Sidebar

Related Questions

I have a database that tracks players through their attempts at a game. To
I have a mySQL database that tracks our projects and drives our website's display
I have this big database table that tracks (currently in production), a persons medical
I'm looking to design an inventory database that tracks a snack bar. As this
We have a database that tracks clicks, so roughly we have a value column
I have a database that tracks employee’s data for the current year and previous
I have been tasked with developing a solution that tracks changes to a database.
I have a high score database for a game that tracks every play in
I am creating a database that will help keep track of which employees have
i have DataBase function that calculate distance by coordinates CREATE OR REPLACE FUNCTION distance(lat1

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.