Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 大小写返回多个值_Sql_Sql Server - Fatal编程技术网

Sql 大小写返回多个值

Sql 大小写返回多个值,sql,sql-server,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].[CustomerBP

我有这个疑问

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'
如果运行此查询,则会出现以下错误: 子查询返回了多个值。当子查询在=、!=、=或者当子查询用作表达式时


如何解决此问题

您可以像这样使用函数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'

考虑为什么子选择可能返回超过1行。在这种情况下,您想做什么?这是案例问题,我只想在案例中运行一个查询。我认为CASECase中不止一个SELECT ar运行有另一种语法,即使用一次值,然后使用多个case值:case value WHEN 1 then。。。当2时,那么…我希望有`如果DATEPARTdw,CBCal.Date=1',那么这个查询`从MyTable中选择[Time From 1],其中[Valid at Monday]=1和[Customer No_]=CBCal.[Customer No_uz]',等等…它不是由该案例引起的。如果将整个CASE表达式替换为MyTable中的[Time From 1],其中[Valid at Monday]=1和[Customer No.]=CBCal.[Customer No.]。也就是说,在查询的某个点上,您需要提供一个生成值的表达式。您已经编写了一些返回多个值的内容。系统不知道该怎么做。我希望所有的数据,而不仅仅是one@kaja只允许所有记录返回分隔的行。@Kaja,当大小写标量表达式返回多行时,是否希望在最终结果中增加行?否则,您将需要返回一个带分隔符的值列表,如Mukesh建议的。