2016-03-20 64 views
1

我正在嘗試編寫一個表比較查詢,當一行中的所有值都不相等時,它將返回通過/失敗列。我有基本的語法,但它是傳遞/失敗整個數據集,而不是特定的行。不知道是否需要在某處使用子查詢。謝謝!使用CASE T SQL表比較SQL

Actual Output: 
TestID LastName FirstName PrimaryLevel Result 
1   Smith  John   1   Fail 
2   Jones  Adam   2   Fail 
3   Barker  Bob   3   Fail 

Expected Output: 
TestID LastName FirstName PrimaryLevel Result 
1   Smith  John   1   Pass 
2   Jones  Adam   2   Fail 
3   Barker  Bob   3   Fail 

--Code: 
declare @TestID int 

declare @TestIDExpected varchar(max) 
declare @LastNameExpected varchar(max) 
declare @FirstNameExpected varchar(max) 
declare @PrimaryLevelExpected varchar(max) 

declare @TestIDActual varchar(max) 
declare @LastNameActual varchar(max) 
declare @FirstNameActual varchar(max) 
declare @PrimaryLevelActual varchar(max) 

set @TestID = 3 

set @TestIDExpected = (select TestID from ExpectedResults where TestID = @TestID) 
set @LastNameExpected = (select LastName from ExpectedResults where TestID = @TestID) 
set @FirstNameExpected = (select FirstName from ExpectedResults where TestID = @TestID) 
set @PrimaryLevelExpected = (select PrimaryLevel from ExpectedResults where TestID = @TestID) 

set @TestIDActual = (select TestID from ActualResults where TestID = @TestID) 
set @LastNameActual = (select LastName from ActualResults where TestID = @TestID) 
set @FirstNameActual = (select FirstName from ActualResults where TestID = @TestID) 
set @PrimaryLevelActual = (select PrimaryLevel from ActualResults where TestID = @TestID) 

select TestID, LastName, FirstName, PrimaryLevel, 
    case 
    when @TestIDExpected = @TestIDActual and @LastNameExpected = @LastNameActual and @FirstNameExpected = @FirstNameActual and @PrimaryLevelExpected = @PrimaryLevelActual then 'Pass' 
    else 'Fail' 
    END as Result from ActualResults 
+0

請您詳細說明一下您的邏輯?爲什麼約翰史密斯期望通過,其他兩個失敗? – Mureinik

+0

沒問題,謝謝。 John Smith將通過,因爲ExpectedResults和Actual Results中的所有返回值均爲=。其他兩行不是。現在看,它可能是TestID設置爲3,當我試圖運行所有3個測試。 – AndrewC10

回答

1

我想你可以用普通的SQL來實現你所需要的。使用LEFT JOIN可以始終返回行,並填充TestResult列,並填充相應的值,指示是否找到匹配項。

如果只有兩個表中的一行與TestID = 3那麼結果將返回一行。您可以修改WHERE子句和您聲明的變量值來滿足您的需求。

聲明變量和值:

DECLARE @TestID INT = 3; 

運行比較查詢:

SELECT 
    a.TestID 
    , a.LastName 
    , a.FirstName 
    , a.PrimaryLevel 
    , CASE WHEN b.TestID IS NOT NULL THEN 'Pass' ELSE 'Fail END AS TestResult 
FROM 
    ActualResults a 
    LEFT JOIN ExpectedResults b ON 
    a.TestID = b.TestID 
    AND a.LastName = b.LastName 
    AND a.FirstName = b.FirstName 
    AND a.PrimaryLevel = b.PrimaryLevel 
WHERE 
    a.TestID = @TestID 

如果你想比較整個數據集,那麼你就可以用WHERE條款沿降DECLARE聲明:

SELECT 
    a.TestID 
    , a.LastName 
    , a.FirstName 
    , a.PrimaryLevel 
    , CASE WHEN b.TestID IS NOT NULL THEN 'Pass' ELSE 'Fail END AS TestResult 
FROM 
    ActualResults a 
    LEFT JOIN ExpectedResults b ON 
    a.TestID = b.TestID 
    AND a.LastName = b.LastName 
    AND a.FirstName = b.FirstName 
    AND a.PrimaryLevel = b.PrimaryLevel 
+0

這有效,但只返回3的TestID。如果我想用TestID 1和2返回3行測試/數據,是否有可能?謝謝 – AndrewC10

+0

正如我在我的回答中所說的,刪除WHERE條件。如果你這樣做,你也不需要聲明的變量。它將掃描整個「ActualResults」表。 –

+0

剛剛看到那部分,非常感謝,完美的作品! – AndrewC10