2013-03-05 14 views
1

我有一個問題,我不能弄明白,它應該是非常快,但我還沒有看到任何像它一樣。因此,這裏是我的主要問題,我有一個定義了基於一個經理團隊,所以它看起來像一個case語句:SQL Server案例陳述

"team" = case 
    when manager = 'manager1' then 'team1' 
    ... 
    when manager = 'managerN' then 'teamN' 
    else 'Other' 
end 

然後我想找到一種方法,使一個新的欄目「凸出」,使其除非項目團隊是「其他」,所以我想它看起來像:

"proj" = case 
    when team = 'Other' then 'Other' 
    else project 
end 

,但我不斷收到錯誤的語法不正確或它說,球隊是不是一個有效的列。有任何想法嗎?

+0

你能分享一個確切的錯誤嗎? – 2013-03-05 22:25:38

+1

您不能在select子句中重用別名。僅在那之後。 – 2013-03-05 22:25:55

+0

列名稱'team'無效。 – NateSHolland 2013-03-05 22:26:12

回答

2
Use Northwind 
GO 



select 

[Hemisphere] = case 
    when derived1.Continent = 'Europe' then 'Eastern' 
    when derived1.Continent = 'North America' then 'Western' 
    when derived1.Continent = 'South America' then 'Western' 
    else 'Other Hemisphere' 
end 
, derived1.Continent 
, derived1.Country 
from (

select c.Country , 
[Continent] = case 
    when c.Country = 'Germany' then 'Europe' 
    when c.Country = 'France' then 'Europe' 
    when c.Country = 'Sweden' then 'Europe' 
    when c.Country = 'Denmark' then 'Europe' 
    when c.Country = 'Finland' then 'Europe' 
    when c.Country = 'Switzerland' then 'Europe' 
    when c.Country = 'Poland' then 'Europe' 
    when c.Country = 'Norway' then 'Europe' 
    when c.Country = 'Ireland' then 'Europe' 
    when c.Country = 'Austria' then 'Europe' 
    when c.Country = 'Italy' then 'Europe' 
    when c.Country = 'Portugal' then 'Europe' 
    when c.Country = 'Belgium' then 'Europe' 
    when c.Country = 'Spain' then 'Europe' 
    when c.Country = 'UK' then 'Europe' 
    when c.Country = 'Spain' then 'Europe' 
    when c.Country = 'Mexico' then 'North America' 
    when c.Country = 'USA' then 'North America' 
    when c.Country = 'Canada' then 'North America' 
    when c.Country = 'Brazil' then 'South America' 
    when c.Country = 'Argentina' then 'South America' 
    when c.Country = 'Venezuela' then 'South America' 
    else 'Other Continent' 
end 

from [dbo].[Customers] c 
) as derived1 
+0

謝謝,這工作。 – NateSHolland 2013-03-05 22:35:23

+0

很高興有幫助。您還有權「標記爲答案」。 :O – granadaCoder 2013-03-05 22:36:20

+0

哈哈我知道,我正在等待時間限制用完。它告訴我,我必須等3分鐘。 – NateSHolland 2013-03-05 22:42:01