SQL Server案例陈述

时间:2013-03-05 22:23:35

标签: sql sql-server

我有一个问题,我无法弄明白,它应该很快但我还没有看到类似的东西。所以这是我的主要问题,我有一个案例陈述,它定义了一个基于经理的团队,所以看起来像:

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

然后我想找到一种方法来创建一个新列“proj”,这样它就是项目,除非团队是'其他'所以我希望它看起来像:

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

但是我不断收到错误,其中语法不正确或者说团队不是有效列。有什么想法吗?

1 个答案:

答案 0 :(得分: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