多个案例语句返回不同的列

时间:2014-08-25 18:33:39

标签: sql tsql select case multiple-columns

问题:我正在做多个案例陈述,但它只返回一列。我想要返回多个列。

当前代码

declare @currentweek as nvarchar(10)
declare @1stweek as nvarchar(10)
declare @2ndweek as nvarchar(10)

set @currentweek= convert(varchar(10),dateadd(ww,0,GETDATE()),110)
set @1stweek= convert(varchar(10),dateadd(ww,1,GETDATE()),110)


select
case 
 when [date] < @currentweek then 'R' 
 when [date] > @currentweek and [date]<@1stweek then 'R' 
else null 
end [current week],[first week]

我想看到的是多列。如果满足条件,则值R将放在[第一周]或[当前周]列中。

由于

1 个答案:

答案 0 :(得分:2)

您需要执行两个不同的Case语句 - 每列一个:

select  case    when [date] < @currentweek  then 'R' 
                else null   
                end as [current week],
        case    when [date] > @currentweek 
                    and [date] < @1stweek   then 'R' 
                else null   
                end as [first week]