Domanda

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.

È stato utile?

Soluzione

You are confused by the window functions, because you can solve this using regular group by:

select SampleId,
       (case when max(TestResult) in ('-99', '00') then max(TestResult)
             when max(TestResult) = min(case when TestResult not in ('-99', '00') then TestResult end)
             then max(TestResult)
             else 'Err '+min(case when TestResult not in ('-99', '00') then TestResult end)+':'+max(TestResult)
        end)
from TempTable
group by SampleId;

There are two important points here. The first is that '-99' < '00' < xx for strings as well as numbers. So, max() works to find the last test result.

The second is finding the "other" value, where there is more than one value. For this, the query uses a conditional minimum -- that is, find the minimum value excluding '-99' and '00'.

The SQL Fiddle is here, which is why I change @TempTable to TempTable.

Altri suggerimenti

Following query will give you maximum row number, but I dont understand the condition for 'Err' can you elaborate it?

Query:

select SampleID, TestResult
from (
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 desc
                        )  AS RowNumber
     FROM @TempTable
  ) t
where RowNumber = 1
ORDER BY SampleID, TestRun
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top