0

I need to write a query that will result with a "fail" condition when one test fails. Testing results are listed in the Fail and Pass columns. One manufacturer product can have many tests. My Data is like this:

MFG Name  | Code | Breaker | Fail | Pass
-----------------------------------------
ABC           R      2        0       1
ABC           R      1        1       0

Regardless of the Breaker value if one test fails, the entire batch fails. The result I was looking for is as follows:

  MFG Name  | Code | Result
-------------------------------------------
ABC             R     FAIL

Any help or insight you can give would be appreciated.

joeldub
  • 57
  • 1
  • 10

3 Answers3

2

Try:

select      mfg_name,
            code,
            case when sum(fail) > 0 then 'FAIL' else 'PASS' end as result
from        tbl
group by    mfg_name,
            code

This assumes you want to show combinations of (mfg_name and code pairs with no fails as a 'PASS', for only fails you would add having sum(fail) > 0)

Brian DeMilia
  • 13,103
  • 1
  • 23
  • 33
0

This is an aggregation query. Assuming fail only takes on two values (0 and 1):

select MFGName, Code,
       (case when max(fail) = 1 then 'FAIL' else 'PASS' end) as Result
from tests
group by MFGName, Code;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Joeldub here you go.

DECLARE @isFail AS INT

@isFail = (SELECT COUNT(FAIL) FROM FIRSTTABLE WHERE MFG NAME = 'ABC' AND FAIL = 1)

IF @isFail > 0
     BEGIN
        INSERT INTO SECONTABLE(with breaker) (Result) VALUES ('FAIL') WHERE WHERE MFG Name = 'ABC'
        ---- OR IF RECORD EXISTS
        UPDATE SECONDTABLE(with breaker) Result = 'FAIL' WHERE MFG Name = 'ABC'
     END
ELSE
     BEGIN
        INSERT INTO SECONTABLE(with breaker) (Result) VALUES ('PASS') WHERE WHERE MFG Name = 'ABC'
        ---- OR IF RECORD EXISTS
        UPDATE SECONDTABLE(with breaker) Result = 'PASS' WHERE MFG Name = 'ABC'
     END
Moojjoo
  • 729
  • 1
  • 12
  • 33