Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server checking for non-equivalent column/rows with grouping
    primarykey
    data
    text
    <p>I would like to process some data in SQL SERVER, do a little QA and get the 'answer' at the same time.</p> <p>The table represents a test and if the test did not give a 100% match, it was run in duplicate. </p> <p>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) </p> <p>So a couple of things I need to do.</p> <p>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 </p> <p>I kinda got the first part with the following code, but I cannot get the MAXIMUM of the ROWNUMBER() part of the query.</p> <p>I am really stuck on the second part.</p> <p>All help would be appreciated ! </p> <pre><code> 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 </code></pre> <p>The results should be like this:</p> <pre><code> 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 </code></pre> <p>**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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload