2016-02-05 123 views
0

我從我的sql server數據庫創建了一個臨時表,如下所示。臨時表中的列標題&描寫的特徵和其他幾個欄目:根據同一表中的另一行更新表格行

Title    Description   ABC Ford Toyota 
Ford Car   This is a Car   A 
Car    This is a Toyota car B 
Toyota Truck  This is a truck   C 
Car    This is a ford car  D 

我需要創建一個輸出,如果標題或描述裏有一句話「福特」和「豐田」在裏面。我需要那個地方的「是」。我需要的結果類似於下面:

*

Title    Description   ABC  Ford Toyota 
Ford Car   This is a Car   A  Yes No 
Car    This is a Toyota car B   No Yes 
Toyota Truck  This is a truck  C   No Yes 
Car    This is a ford car  D  Yes No 
car    ford toyota   E  Yes Yes 

*

能否請你讓我知道這將如何工作的?任何指針?我有點堅持在這裏..

回答

2

使用CASE表達:

UPDATE tbl 
    SET Ford = CASE WHEN Title LIKE '%Ford%' OR Description LIKE '%Ford' THEN 'YES' ELSE 'NO' END, 
     Toyota = CASE WHEN Title LIKE '%Toyota%' OR Description LIKE '%Toyota' THEN 'YES' ELSE 'NO' END 
+0

織補..被毆打到:( – 2016-02-05 01:01:55

0

您可以從自身更新表,我下面的查詢提供你所描述的例子:

CREATE TABLE #Temp (
Title NVARCHAR(20) NULL, 
Description NVARCHAR(50) NULL, 
ABC NVARCHAR(1) NULL, 
Ford NVARCHAR(3) NULL, 
Toyota NVARCHAR(3) NULL) 

INSERT INTO #Temp VALUES 
('Ford Car','This is a Car','A',NULL,NULL), 
('Car','This is a Toyota car','B',NULL,NULL), 
('Toyota Truck','This is a truck','C' ,NULL,NULL), 
('Car','This is a ford car','D' ,NULL,NULL)  

UPDATE #Temp 
SET Ford = IIF(#Temp.Title LIKE '%Ford%' OR #Temp.Description LIKE '%Ford%','Yes','No') 
    ,Toyota = IIF(#Temp.Title LIKE '%Toyota%' OR #Temp.Description LIKE '%Toyota%','Yes','No') 
相關問題