I've got a query I wrote using a JOIN to display a summary using the part's Serial_Number.
Obviously, it does not work!
Using the direct search here:
select Serial_Number, Op_ID, Date_Time as 'DecayDate', 
    DECAY.System_ID AS 'DecayID', Test_Result as 'DecayResult' 
from Test_Results
where serial_number='CP21295 1006 09' and system_id like '%decay%'
I get this sample output:
Serial_Number Op_ID DecayDate DecayID DecayResult CP21295 1006 09 003468 2009-10-15 06:36:13.643 AA_DECAY_6 FAILED CP21295 1006 09 003468 2009-10-05 08:08:38.503 AA_DECAY_6 PASSED
Notice there were two (2) tests for this one part number that I need the result of.
Now to the query I created that does not work:
This first version will return ONE record:
SELECT LABEL.Serial_Number, 
  DECAY.Op_ID,
  DECAY.Date_Time AS 'DecayDate',
  DECAY.System_ID AS 'DecayID',
  CASE
    WHEN DECAY.Test_Result LIKE '%fail%' THEN 'FAIL'
    WHEN DECAY.Test_Result LIKE '%pass%' THEN 'PASS'
    ELSE NULL
  END AS 'DecayResult'
  FROM 
  ACP_Parts AS LABEL
  LEFT OUTER JOIN (
    SELECT I.Serial_Number, I.Op_ID, I.Date_Time, I.System_ID, I.Test_Result
        FROM Test_Results I
    LEFT OUTER JOIN Test_Results O ON (
        I.Serial_Number=O.Serial_Number) AND 
        (O.Date_Time<I.Date_Time) -- AND (O.Serial_Number IS NULL) // didn't work
    WHERE
        (I.Test_Result LIKE '%pass%' OR I.Test_Result LIKE '%fail%') AND 
        (I.System_ID like '%decay%') AND
        (O.Test_Result LIKE '%pass%' OR O.Test_Result LIKE '%fail%') AND
        (O.System_ID like '%decay%')
    ) AS DECAY ON 
        (LABEL.Serial_Number=DECAY.Serial_Number) AND 
        (LABEL.Date_Time<DECAY.Date_Time)
  WHERE 
    (LABEL.Serial_Number IN (
        SELECT DISTINCT Serial_Number 
        FROM ACP_Parts 
        WHERE (Serial_Number IS NOT NULL) AND 
         (DATEADD(yy, - 1, GETDATE()) < Date_Time)))
Output
Serial_Number DecayOp DecayDate DecayID DecayResult CP21295 1006 09 003468 2009-10-15 06:36:13.643 AA_DECAY_6 PASS
I am guessing the above query produces a result because there are two or more records for it to compare. Right?
I modified the query above, hoping to return the newest record, but this version does not return anything:
SELECT LABEL.Serial_Number, 
  DECAY.Op_ID,
  DECAY.Date_Time AS 'DecayDate',
  DECAY.System_ID 'DecayID',
  CASE
    WHEN DECAY.Test_Result LIKE '%fail%' THEN 'FAIL'
    WHEN DECAY.Test_Result LIKE '%pass%' THEN 'PASS'
    ELSE NULL
  END AS 'DecayResult'
  FROM 
  ACP_Parts AS LABEL
  LEFT OUTER JOIN (
    SELECT TOP 1 Serial_Number, Op_ID, Date_Time, System_ID, Test_Result
    FROM Test_Results
    WHERE
      (System_ID like '%decay%') AND
      (Test_Result LIKE '%pass%' OR Test_Result LIKE '%fail%')
    ORDER BY Date_Time DESC
    ) AS DECAY ON 
        (LABEL.Serial_Number=DECAY.Serial_Number) AND 
        (LABEL.Date_Time<DECAY.Date_Time)
  WHERE 
    (LABEL.Serial_Number IN (
        SELECT DISTINCT Serial_Number 
        FROM ACP_Parts 
        WHERE (Serial_Number IS NOT NULL) AND 
         (DATEADD(yy, - 1, GETDATE()) < Date_Time)))
Output
Serial_Number DecayOp DecayDate DecayID DecayResult CP21295 1006 09 NULL NULL NULL NULL
Could someone point out how I could get this query to return the single result that I'm after?