I have a master item ship/return table where we keep track of every SN shipped (date it was shipped, where it was shipped to, and if the SN was returned). We have some records that show a SN was shipped, then shipped again without the previous return info. This is because someone entered the SN as shipped when it actually was not shipped. I need to write a MySQL query that will show all shipped SN’s but not include the record without the return data when it was shipped after.
Here is an example of the data from my query:
Select * From database1.Deployed_Returned group by SN, RMA_No ORDER BY SN, Date_Shipped;
SN | Date_Shipped | Ship_to_Store | Date_Received | Received_from_Store | RMA_No |
---|---|---|---|---|---|
1111 | 4/28/2021 | LA_7146 | 6/9/2021 | LA_7146 | 12356 |
1111 | 6/22/2021 | Denver_2114 | 8/18/2021 | Denver_2114 | 12557 |
1111 | 8/20/2021 | NY_7890 | NULL | NULL | NULL |
1111 | 9/29/2021 | Miami_9821 | 11/30/2022 | Miami_9821 | 13029 |
1111 | 12/5/2022 | Atlanta_1278 | NULL | NULL | NULL |
2222 | 5/12/2022 | Chicago_3476 | 11/24/2022 | Chicago_3476 | 12987 |
2222 | 12/5/2022 | Denver_2114 | NULL | NULL | NULL |
3333 | 1/20/2023 | LA_7146 | NULL | NULL | NULL |
SN: 1111, Date_Shipped: 8/20/2021, Date_Received: NULL. This record needs to be removed (because the next record shows it shipped on 9/29/2022)
2
Answers
You are looking for a
self join.
To begin, create a temp reporting table that will simplify your life:
Giving it a proper index wouldn’t hurt, either.
Sometimes CREATE TABLE … followed by INSERT INTO …
is the most convenient way to arrange that.
Now identify bogus records according to your business logic.
The OP did not explain exactly what that is.
Here is one take on it.
Possibly
RMA_No
should also be involved.You may also find it helpful to display selected
r2
detailssuch as the date received.
Verify the business logic, and then
use those result rows to DELETE rows that are bogus.
You may find it helpful to add an
is_bogus
booleancolumn, so you can try some UPDATE commands and
then finally when you’re confident of the result do a
DELETE … WHERE is_bogus;
If you are using MySQL 8.0 you can use CTE – 13.2.20 WITH Common Table Expressions.
So, at the first step you find maximal
Date_Shipped
for eachSN
which is the date of latest shipped and not returned item.Then you find records for each item where
Date_Received
isNULL
butDate_Shipped
is less then maximal value for found item.