2016-08-19 31 views
-3

我有表ClassificationCode,它有列:SQL Server的列

  • CodeName和代碼我有
  • CodeType和1至5的代碼的其大陸型的其大陸名稱和每個數字參考代碼命名

當我用where條件指定名稱它的工作原理

Select code name 
from classification code 
where code type = 2 ; 

我的問題是,如果我能在不同的條件下重複的代碼名稱這樣

Select 
    code name, code name as 2, code name as 3 
from 
    classification code 
where 
    code type = 2 And code type = 3 and code type=4... 
+0

你想完成什麼? –

+0

這裏你的意思並不完全清楚。這將是一個很好的開始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

+0

查詢更多三次使用deffrent數據選擇代碼名稱 –

回答

0

如果我正確理解你的問題,你有兩個選擇。首先,如果你想多行,使用in(這相當於or代替and):

select code_name from classification_code where code_type in (2,3,4) 

或者,如果你想pivot你的結果爲單行,這裏是一個使用conditional aggregation一個選項:

select max(case when code_type = 2 then code_name end) code_name_1, 
     max(case when code_type = 3 then code_name end) code_name_2, 
     max(case when code_type = 4 then code_name end) code_name_3 
from classification_code 
where code_type in (2,3,4) 
+0

非常感謝它的工作:-) :-) –