繁体   English   中英

带有大小写的SQL Server子查询

[英]sql server subquery with case

我有以下sql:

    select * from 
    ( 
         select p.ID, 
         Received = (select Rec from Exp   
                     where EstAmt = (select MAX(ex.EstAmt) from Exp ex 
                                     where ex.Prot = p.ID and EstAmt > 0) 
                    )    

   From Prot  
   ) subsel 
   where Received = 1 

我想对接收到的数据进行处理,因此如果它为1,则说“是”,否则说“否”。 我知道该怎么办,但在这种情况下不知道,因为我收到了这是一个子查询。

我尝试了以下操作,但是没有用

     select * from 
     ( 
       select p.ID, 

       case Received = (select Rec from Exp   
                        where EstAmt = (select MAX(ex.EstAmt) from Exp ex 
                                        where ex.Prot = p.ID and EstAmt > 0) 
                  )    
      when 1 then 'Yes'
      else 'No'
      end Received
      From Prot  
     ) subsel 
    where Received = 1 

代替

case Received = (select Rec from Exp   
                        where EstAmt = (select MAX(ex.EstAmt) from Exp ex 
                                        where ex.Prot = p.ID and EstAmt > 0) 
                  )  

尝试:

case (select Rec from Exp   
                            where EstAmt = (select MAX(ex.EstAmt) from Exp ex 
                                            where ex.Prot = p.ID and EstAmt > 0) 
                      )  

其他答案也很重要,但是您不能将子查询的值分配给Received,只能在“ case”之后使用单个值,并确保子查询不返回多个值。

我认为您需要在“结束”之后删除“已接收”。 IE浏览器。 更改:

when 1 then 'Yes'
else 'No'
end Received

when 1 then 'Yes'
else 'No'
end

就像在过滤器中一样,在子查询之外获取这种case

    select subsel.ID,
    case subsel.Received 
      when 1 then 'Yes'
      else 'No'
    end Received 
    from 
    ( 
         select p.ID, 
         Received = (select Rec from Exp   
                     where EstAmt = (select MAX(ex.EstAmt) from Exp ex 
                                     where ex.Prot = p.ID and EstAmt > 0) 
                    )    

   From Prot  
   ) subsel 
   where Received = 1 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM