Question: I'm completely useless regarding databases, but currently I'm having to work with it.

I need to make a query that compares date values between to different entries of my table. I have a query like this:

```
SELECT t1.serial_number, t1.fault_type, t2.fault_type 
FROM  shipped_products t1 
      JOIN shipped_products t2 ON t1.serial_number=t2.serial_number 
WHERE ABS(DATEDIFF(t2.date_rcv,t1.date_rcv))<90;

```

But it's taking forever to run. Really, I left it running for 18 hours and it never stoped. Is this query correct? Is there a better, more clever way to do this?

Thank you very much guys.

BTW: I'll automate all the process with python scripts, so if you know of a better way to do this inside python without all the logic having to be inside the query, it would also help.

EDIT:
My question seems unclear, so I'll explain better what I need to do.
I have a problem that sometimes products go to repair centers and are shipped back to clients as "No Deffect found". After that the client ship it againg to repair centers for they present the same issue. So i need a query to count how many products have been to repair centers twice in an interval of 90 days. The unic ID for each single product is its serial number, and that's why I'm searching for sereal number duplicates.

Answer: 
Every record is going to match itself (in t1 and t2) in this join since the DateDiff will be the same and thus less than 90. Make sure you are not matching to the same record. If you have an ID field in your table you could do this:

```
SELECT t1.serial_number, t1.fault_type, t2.fault_type 
FROM  shipped_products t1 
      JOIN shipped_products t2 
      ON t1.serial_number=t2.serial_number 
      AND t1.ID <> t2.ID
WHERE ABS(DATEDIFF(t2.date_rcv,t1.date_rcv))<90;

```

Also make sure you have a key on serial\_number.
It is unclear to me why you would want duplicates in the results. If you have two rows that meet the condition, then both will be in the result set. Why not just look at records that come later? If you phrase the query like this:

```
SELECT t1.serial_number, t1.fault_type, t2.fault_type 
FROM  shipped_products t1 JOIN
      shipped_products t2
     ON t1.serial_number = t2.serial_number 
WHERE t2.date_recv >= t1.date_rcv and
      t2.date_recv < t1.date_recv + interval 90 day;

```

Then the resulting query can take advantage of an index on `shipped_products(serial_number, date_recv)`. Note: Perhaps the 90 should be 180.

I am suspicious when I see this type of self-join. Sometimes, it can be replaced with an aggregation query (sometimes not). However, what you actually want to do is unclear.