我想要做的是這樣的:在很多條件下使用SQL的CASE?
case_1 case_2 case_3 Final
0 0 0 0
0 0 1 3
0 1 0 2
1 0 0 1
1 1 0 2
1 0 1 3
0 1 1 3
1 1 1 3
這時候case_1爲0表示,case_2是0和case_3是0,最終山坳具有值爲0 同樣,當case_1是1,case_2爲1並且case_3是1,最後的列將是3. 等等。
什麼我結束了打字SQL這是尷尬的:
Select *,
case when case_1>0 and case_2>0 and case_3>0 then 3 else 0,
case when case_1>0 and case_2>0 and case_3=0 then 2 else 0,
case when case_1>0 and case_2=0 and case_3=0 then 1 else 0,
....
....
....
from mytable;
現在,這是嚴重不利的,我知道。有沒有更好的編碼方式?
這些列可以包含除「0」和「1」以外的任何內容嗎? – Barmar
@Barmar只有0,1,2和3爲最後一列,其餘三個有0或1 –
我真的不明白。爲什麼你想要的輸出只有一個'Final'列,但你的查詢有多個'case'表達式?哪一個纔是真正的'Final'? – Barmar