2013-05-20 67 views
1

好的,只是關於使用CASE函數的一個簡短問題。不知道我是否能像我在下面的例子中看到的那樣使用它,但是我想根據區域和銀行代碼來使用它。有人可以請這樣做的語法協助。基於多個列的案例功能

表:BankInfo

BankCode  PayeeName  District 
    101   John Doe   333 
    101   Doe John   233 
    100   Jane Doe   333 

查詢:

SELECT 
    PayeeName, BankCode 
    ,CASE dbo.BankInfo.district 
     WHEN 333 and BankCode = 101 THEN '79999' 
    END as BankTransit 

回答

3

由於您使用的兩列,您需要將其更改爲:

SELECT 
    PayeeName, BankCode, 
    CASE 
     WHEN dbo.BankInfo.district = 333 AND BankCode = 101 
     THEN '79999' 
    END as BankTransit 
+0

試過這個,但得到一個錯誤「一個非布爾類型的表達式在一個上下文中預期的條件,靠近'和'」 –

0

一通用Northwind示例:

Select 
CustomerID , PostalCode , 
[MyMatch] = 
CASE 
    When CustomerID = 'ALFKI' and PostalCode = '12209' then 1 
    else 0 
END 

from [dbo].[Customers] 

Order by 3 DESC ,1,2 

和翻譯........ 我有一個別名「MyDerived1Value」。還有一個「別的」聲明。

Select 
PayeeName, BankCode , 
[MyDerivedValue] = 
CASE 
    When District = '333' and BankCode = '101' then '79999' 
    else null 
END 

from [dbo].BankInfo 

Order by 3 DESC ,1,2 

如果District和/或BankCode是int,則刪除單引號。