I have the following table of a mysql product movement database that contains the following fields:
id | prod_id | prod_name | movimento_tipo | data_transaction
1 | idProd1 | tv | ordemCompra | 1-jan-2018
2 | idProd1 | tv | shipped | 3-jan-2018
3 | idProd2 | pc | shipped | 4-jan-2018
4 | idProd2 | pc | ordemCompra | 5-jan-2018
5 | idProd3 | sapato | return | 8-jan-2018
6 | idProd3 | sapato | ordemCompra | 7-jan-2018
7 | idProd4 | camisa | ordemCompra | 10-jan-2018
8 | idProd4 | camisa | ordemCompra | 12-jan-2018
The tv product (idProd1) is correct. Had a purchase order on Jan 1 and shipping happened on Jan 3. The pc product (idProd2) has an error because shipping came before (4-Jan) than the purchase (5-Jan). In the same way the shoe had a return before the purchase. The shirt (idProd4) had 2 purchase orders but did not have a return between these two purchases.
My question is what would be the sql query to catch each of these errors. I can not use any languages in the backend (php or python for example) to filter these situations.