我正在嘗試編寫一個表比較查詢,當一行中的所有值都不相等時,它將返回通過/失敗列。我有基本的語法,但它是傳遞/失敗整個數據集,而不是特定的行。不知道是否需要在某處使用子查詢。謝謝!使用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
請您詳細說明一下您的邏輯?爲什麼約翰史密斯期望通過,其他兩個失敗? – Mureinik
沒問題,謝謝。 John Smith將通過,因爲ExpectedResults和Actual Results中的所有返回值均爲=。其他兩行不是。現在看,它可能是TestID設置爲3,當我試圖運行所有3個測試。 – AndrewC10