Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 960127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:07:57+00:00 2026-05-16T01:07:57+00:00

I’ve got a query I wrote using a JOIN to display a summary using

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:07:57+00:00Added an answer on May 16, 2026 at 1:07 am

    OK, I’ve found ways to tweak the code to produce my desired results.

    First, here is the data I used:

    CREATE TABLE JP1 (Serial_Number VarChar(20), Date_Time DateTime, Test_Result VarChar(255))
    INSERT JP1 VALUES ('T900 09', '2009-10-10 01:15:00.000', 'NO TEST, Failed to Dump')
    INSERT JP1 VALUES ('T901 09', '2009-10-10 01:20:00.000', '(BLUE) PASS (WHITE) FAIL')
    
    CREATE TABLE JP2 (Serial_Number VarChar(20), Date_Time DateTime, Test_Result VarChar(255))
    INSERT JP2 VALUES ('T900 09', '2009-10-15 01:15:00.000', 'NO TEST, Failed to Dump')
    INSERT JP2 VALUES ('T900 09', '2009-10-15 02:15:00.000', '(BLUE) PASS (WHITE) FAIL')
    INSERT JP2 VALUES ('T900 09', '2009-10-15 04:25:00.000', 'TEST ABORTED')
    INSERT JP2 VALUES ('T900 09', '2009-10-15 04:59:00.000', '(BLUE) FAIL (WHITE) FAIL')
    INSERT JP2 VALUES ('T900 09', '2009-10-15 05:09:00.000', '(WHITE) NO TEST')
    INSERT JP2 VALUES ('T900 09', '2009-10-15 05:34:00.000', 'B=Pass.,W=FAIL, Final Fill')
    INSERT JP2 VALUES ('T900 09', '2009-10-15 06:06:00.000', 'PASSED PD')
    INSERT JP2 VALUES ('T900 09', '2009-10-15 06:30:00.000', 'TEST ABORTED')
    INSERT JP2 VALUES ('T900 09', '2009-10-15 06:36:00.000', 'PASS')
    INSERT JP2 VALUES ('T900 09', '2009-10-15 06:50:00.000', 'FAIL')
    INSERT JP2 VALUES ('T900 09', '2009-10-05 08:08:00.000', 'NO TEST, Operator cancel')
    

    Next, I created two (2) very similar queries. It turns out, the requirements I set forth in the LEFT OUTER JOIN clause needs to be mimicked in the WHERE clause.

    Here is how I extract the FIRST PASS result and date:

    SELECT LABEL.Serial_Number, 
        DECAY.Date_Time AS 'DecayDate',
        CASE
        WHEN DECAY.Test_Result LIKE '%fail%' THEN 'FAIL'
        WHEN DECAY.Test_Result LIKE '%pass%' THEN 'PASS'
        ELSE NULL
        END AS 'DecayResult'
    FROM JP1 AS LABEL
    LEFT OUTER JOIN (
        SELECT I.Serial_Number, I.Date_Time, I.Test_Result
        FROM
            JP2 AS I
            LEFT OUTER JOIN JP2 AS O
            ON
            (I.Serial_Number=O.Serial_Number) AND
            ((O.Test_Result LIKE '%pass%') OR (O.Test_Result LIKE '%fail%')) AND -- //Gets First Test!
            (O.Test_Result NOT LIKE '%NO TEST%') AND
            (O.Date_Time<I.Date_Time) -- //(selects FIRST pass or fail)
            -- (I.Date_Time<O.Date_Time) -- //(selects LAST pass or fail)
        WHERE
            (I.Test_Result LIKE '%pass%' OR I.Test_Result LIKE '%fail%') AND -- //Gets First Test!
            (I.Test_Result NOT LIKE '%NO TEST%') AND
            (O.Serial_Number IS NULL)
    ) 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 JP1
        WHERE (Serial_Number IS NOT NULL) AND 
        (DATEADD(yy, - 1, GETDATE()) < Date_Time))
    )
    

    Here is how I extract the FIRST Test result and date:

    SELECT LABEL.Serial_Number, 
        DECAY.Date_Time AS 'DecayDate',
        CASE
        WHEN DECAY.Test_Result LIKE '%fail%' THEN 'FAIL'
        WHEN DECAY.Test_Result LIKE '%pass%' THEN 'PASS'
        ELSE NULL
        END AS 'DecayResult'
    FROM JP1 AS LABEL
    LEFT OUTER JOIN (
        SELECT I.Serial_Number, I.Date_Time, I.Test_Result
        FROM
            JP2 AS I
            LEFT OUTER JOIN JP2 AS O
            ON
            (I.Serial_Number=O.Serial_Number) AND
            ((O.Test_Result LIKE '%pass%') AND (O.Test_Result NOT LIKE '%fail%')) AND -- //Gets First Pass!
            (O.Test_Result NOT LIKE '%NO TEST%') AND
            (O.Date_Time<I.Date_Time) -- //(selects FIRST pass or fail)
            -- (I.Date_Time<O.Date_Time) -- //(selects LAST pass or fail)
        WHERE
            (I.Test_Result LIKE '%pass%' AND I.Test_Result NOT LIKE '%fail%') AND -- //Gets First Pass!
            (I.Test_Result NOT LIKE '%NO TEST%') AND
            (O.Serial_Number IS NULL)
    ) 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 JP1
        WHERE (Serial_Number IS NOT NULL) AND 
        (DATEADD(yy, - 1, GETDATE()) < Date_Time))
    )
    

    I hope others find this useful!

    ~Joe

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.