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.
The trick here is to stipulate ‘and there is no other lost or found date between the lost and found dates’, or, in SQL:
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:
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.