CASE返回多个值

时间:2016-04-14 06:33:45

标签: sql sql-server

我有这个查询

select a.Tag,a.Type, a.[Starting Date], a.[Time From 1], a.[Time To 1], 
       DATEPART(dw,CBCal.Date) as [TagderWoche], CBCal.Date, CBCal.[Customer No_],
       Description, [POS Holiday] 
       from [ReplicationLayer].[BackPro].[CustomerBPCal] as CBCal 
CROSS APPLY
(
    select Type,[Starting Date],[Time From 1],[Time To 1] ,
    (case 
         WHEN DATEPART(dw,CBCal.Date)=1 then (select [Time From 1] from MyTable 
                                              where [Valid at Monday]=1 
                                              and [Customer No_]=CBCal.[Customer No_] ) 
         WHEN DATEPART(dw,CBCal.Date)=3 then (select [Time From 1] from MyTable 
                                              where [Valid at Wednesday]=1 
                                              and [Customer No_]=CBCal.[Customer No_]) 
         WHEN DATEPART(dw,CBCal.Date)=4 then (select [Time From 1] from MyTable 
                                              where [Valid at Thursday]=1 
                                              and [Customer No_]=CBCal.[Customer No_])
         WHEN DATEPART(dw,CBCal.Date)=5 then (select [Time From 1] from MyTable 
                                              where [Valid at Friday]=1 
                                              and [Customer No_]=CBCal.[Customer No_]) 
         WHEN DATEPART(dw,CBCal.Date)=6 then (select [Time From 1] from MyTable 
                                              where [Valid at Saturday]=1 
                                              and [Customer No_]=CBCal.[Customer No_]) 
         WHEN DATEPART(dw,CBCal.Date)=7 then (select [Time From 1] from MyTable 
                                              where [Valid at Sunday]=1 
                                              and [Customer No_]=CBCal.[Customer No_])
    end) as Tag
    from  [CustomerShopAndArrivalTime]
) as a
where CBCal.[Customer No_]=1 and CBCal.[POS Holiday]=0 and Date='2015-04-15'

如果我运行此查询,我收到此错误:     子查询返回的值超过1。当子查询跟随=,!=,=或子查询用作表达式时,不允许这样做。

我该怎么做才能解决这个问题

1 个答案:

答案 0 :(得分:0)

您可以像这样使用函数max,min或top

    select a.Tag,
           a.Type,
           a.[Starting Date],
           a.[Time From 1],
           a.[Time To 1], 
           DATEPART(dw,CBCal.Date) as [TagderWoche], 
           CBCal.Date, 
           CBCal.[Customer No_],
           Description,[POS Holiday] 
from [ReplicationLayer].[BackPro].[CustomerBPCal] as CBCal 
    CROSS APPLY
    (
    select Type,[Starting Date],[Time From 1],[Time To 1] ,
    (case 
         WHEN DATEPART(dw,CBCal.Date)=1 then 
                      (select top 1 [Time From 1] from MyTable where [Valid at Monday]=1 and [Customer No_]=CBCal.[Customer No_] ) 
         WHEN DATEPART(dw,CBCal.Date)=3 then  
                      (select top 1 [Time From 1] from MyTable where [Valid at Wednesday]=1 and [Customer No_]=CBCal.[Customer No_]) 
         WHEN DATEPART(dw,CBCal.Date)=4 then  
                      (select top 1 [Time From 1] from MyTable where [Valid at Thursday]=1 and [Customer No_]=CBCal.[Customer No_])
         WHEN DATEPART(dw,CBCal.Date)=5 then 
                      (select top 1 [Time From 1] from MyTable where [Valid at Friday]=1 and [Customer No_]=CBCal.[Customer No_]) 
         WHEN DATEPART(dw,CBCal.Date)=6 then  
                      (select top 1 [Time From 1] from MyTable where [Valid at Saturday]=1 and [Customer No_]=CBCal.[Customer No_]) 
         WHEN DATEPART(dw,CBCal.Date)=7 then 
                      (select top 1 [Time From 1] from MyTable where [Valid at Sunday]=1 and [Customer No_]=CBCal.[Customer No_])
      end) as Tag
        from  [CustomerShopAndArrivalTime]
    ) as a
    where CBCal.[Customer No_]=1 and CBCal.[POS Holiday]=0 and Date='2015-04-15'