I would like to process some data in SQL SERVER, do a little QA and get the 'answer' at the same time.
The table represents a test and if the test did not give a 100% match, it was run in duplicate.
In this table the following codes are applicable and they are in ascending order of priority:
1) -99 test was skipped
2) 00 test was not 100% match
3) Anything > 00 is 100% match (e.g. 01, 02, 01-01 etc)
So a couple of things I need to do.
First, for each SampleID I need to pick out where 01 > 00 > -99
Second, I need to know if any test had 100% match (e.g. #3) and the tests disagree. An example of this is 11-0010-P1 (below) where Run2=01 and Run3=02
I kinda got the first part with the following code, but I cannot get the MAXIMUM of the ROWNUMBER() part of the query.
I am really stuck on the second part.
All help would be appreciated !
DECLARE @TempTable TABLE (SampleID varchar(10), TestRun int, TestResult varchar(max))
INSERT INTO @TempTable VALUES('11-0003-P1', 1,'-99')
INSERT INTO @TempTable VALUES('11-0004-P1', 1, '00')
INSERT INTO @TempTable VALUES('11-0005-P1', 1, '01')
INSERT INTO @TempTable VALUES('11-0007-P1', 1,'-99')
INSERT INTO @TempTable VALUES('11-0007-P1', 2, '00')
INSERT INTO @TempTable VALUES('11-0007-P1', 3, '00')
INSERT INTO @TempTable VALUES('11-0008-P1', 1,'-99')
INSERT INTO @TempTable VALUES('11-0008-P1', 2, '02')
INSERT INTO @TempTable VALUES('11-0008-P1', 3, '02')
INSERT INTO @TempTable VALUES('11-0009-P1', 1, '00')
INSERT INTO @TempTable VALUES('11-0009-P1', 2, '07')
INSERT INTO @TempTable VALUES('11-0009-P1', 3, '07')
INSERT INTO @TempTable VALUES('11-0010-P1', 1, '00')
INSERT INTO @TempTable VALUES('11-0010-P1', 2, '01')
INSERT INTO @TempTable VALUES('11-0010-P1', 3, '02')
SELECT SampleID, TestRun, TestResult,
ROW_NUMBER() OVER (PARTITION BY SampleID
ORDER BY
CASE TestResult
WHEN '-99' THEN 1
WHEN '00' THEN 2
ELSE 3
END
) AS RowNumber
FROM @TempTable
ORDER BY SampleID, TestRun
The results should be like this:
SampleID TestResult
11-0003-P1 -99
11-0004-P1 00
11-0005-P1 01
11-0007-P1 00
11-0008-P1 02
11-0009-P1 07
11-0010-P1 Err 01:02
**it would ideal to actually give the conflicting values (e.g. 01:02 for 11-0010-P1) but not totally necessary, the Err flag would be OK.