計算欄位布林值的T-SQL寫法?

  • 5417
  • 0

摘要:計算欄位布林值的T-SQL寫法?

問題:
假設某一Table有五個欄位:A,B,C,D,E,
型別皆為Integer.
我想要取得每ㄧ筆Record中的最大值,
且判斷此最大值是否是欄位E.
如果確實是欄位E的話,
則第六個欄位F的值為1(True),
否則為0(False).
Example:
A,B,C,D,E,F
1,2,3,4,5,True
這樣的計算欄位如何以T-SQL表達呢?

回覆:
根據阿尼大大跟Charles Lin大大的語法,
小弟做了一點修正如下

DECLARE @test TABLE( A INT, B INT, C INT, D INT, E INT)

INSERT INTO @test VALUES ( 1, 2, 3, 4, 5)
INSERT INTO @test VALUES ( 5, 4, 3, 4, 3)
INSERT INTO @test VALUES ( 1, 5, 3, 4, 2)

SELECT max(ta.A) as maxA, max(tb.B) as maxB, max(tc.C) as maxC, max(td.D) as maxD, max(te.E) as maxE,
		(CASE WHEN max(te.E)>=max(ta.A) and max(te.E)>=max(tb.B) and max(te.E)>=max(tc.C) and max(te.E)>=max(td.D)
		THEN 'true' 
		ELSE 'false' 
		END) AS F
FROM @test as ta,
	@test as tb,
	@test as tc,
	@test as td,
	@test as te

引用自此