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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:19:54+00:00 2026-05-25T10:19:54+00:00

I have a table with entries for Items as being ‘lost’ and ‘found’. Each

  • 0

I have a table with entries for Items as being ‘lost’ and ‘found’. Each row has a date for the event. Im hoping to build a query with matching pairs of ‘itemid’, ‘lost date’, ‘found date’ by joining the table to itself.

This works to a point: unfortunately if there are multiple lost and found pairs for a given item each ‘lost date’ will be joined with all the ‘found dates’ that follow it.

Still with me?

The query goes something like:

select c0.ItemId, c0.ChangeDate, c1.ChangeDate from Changes c0
join Changes c1 on 
   c0.ItemId = c1.ItemId and c1.ChangeDate >= c0.ChangeDate
where c0.ChangeType = 9 (lost) and c1.ChangeType = 10 (found);

What Im hoping to achieve is some form of a given ‘lost date’ paired with only the next ‘found date’ in sequence (or NULL if no ‘found date’ exists). Im (pretty) sure this is possible but Im not seeing the path.

I was wondering about putting a sub-select in the first join and using a LIMIT 1 to get only one record but I don’t see how to join this to the appropriate row in the main part of the select. MySQL tells me it doesn’t exist. Fair enough.

  • 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-25T10:19:55+00:00Added an answer on May 25, 2026 at 10:19 am

    The trick here is to stipulate ‘and there is no other lost or found date between the lost and found dates’, or, in SQL:

    SELECT c0.ItemId, c0.ChangeDate, c1.ChangeDate
      FROM Changes AS c0
      JOIN Changes AS c1 ON c0.ItemId = c1.ItemId AND c1.ChangeDate >= c0.ChangeDate
     WHERE c0.ChangeType = 9   -- Lost
       AND c1.ChangeType = 10  -- Found
       AND NOT EXISTS(SELECT *
                        FROM Changes AS c2
                       WHERE c2.ItemId = c1.ItemID
                         AND c2.ChangeType IN (9, 10) -- Lost or Found
                         AND c2.ChangeDate BETWEEN c0.ChangeDate AND c1.ChangeDate
                         AND (c2.ChangeDate != c0.ChangeDate AND c2.ChangeDate != c1.ChangeDate)
                     );
    

    Because that is a correlated sub-query, it tends to slow down the query, but it should produce the correct rows.

    There is an important caveat about the way I’ve eliminated the c0 and c1 rows by stipulating that the ChangeDate for the row in c2 should be different from either the lost date or the found date. However, the main query seems to allow for an item to be found on the same day that it is lost. There might be some other column – such as a ChangeId column – that is not mentioned in the query yet that could be used instead:

    AND c2.ChangeID NOT IN (c0.ChangeID, c1.ChangeID)
    

    You’ll need to think about what happens if an item is lost on, say, 2011-06-07, and lost again on 2011-06-14, and only found on 2011-06-21. And what about if it is also found on 2011-06-28? Such problems should be prevented by the data entry processing, so the query above assumes there won’t be such issues.

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

Sidebar

Related Questions

I have a table y Which has two columns a and b Entries are:
I have table with 50 entries (users with such details like Name Surname Location
I have table with a unique auto-incremental primary key. Over time, entries may be
I have a simple database table called Entries: class CreateEntries < ActiveRecord::Migration def self.up
I have a table containing hundreds of entries and I am trying to delete
I have a table that is a collection entries as to when a user
I have a table that contains log entries for a program I'm writing. I'm
I have a table story_category in my database with corrupt entries. The next query
I have a table of transactions which will occasionally have duplicate entries. If/When an
I have a MySQL table that holds many entries with repeated IDs (for various

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.