具体代码为:
select bc2.nomb,
         (case when getdate(bc2.putdate,bc2.matdate) ='01'  then
                (select ri.TeamRate 
                 from rate_info ri 
                 where ri.rateyear>= replace(bc2.putoutdate,'/','-')
                 and ri.teamtime ='1'
                 and rownum=1
                 order by ri.rateyear asc)         
            else 0   end
          ) businessrate
         from  bc2,
            lc2
           where bc2.nomb = lc2.nomb
           and   bc2.nomb not in(select bd2.no2 from  bd2)
           and bc2.putdate is not null
           and bc2.matdate is not null; 而下面语句确可以正确执行:select ri.TeamRate 
                 from rate_info ri 
                 where ri.rateyear>= replace(bc2.putoutdate,'/','-')
                 and ri.teamtime ='1'
                 and rownum=1
                 order by ri.rateyear asc我子查询的主要目的是查找大于某个日期的最大利率还有没有什么好的方法可以改进这个子查询,能达到我要求的目标。

解决方案 »

  1.   

    case 中 then 后面需要的数据 是单一数据 不能有多于1个的值为什么不用 select max(TeamRate )?
      

  2.   

    说错了,应当是排序之后的最小值。我的case里还有许多其它情况,只是举一个例子。按日期排序,然后取与最小日期对应的利率。
      

  3.   

     使用order by 请与group by 搭配使用
      

  4.   

    用上GROUP BY 应该 就可以了!
    select bc2.nomb, 
            (case when getdate(bc2.putdate,bc2.matdate) ='01'  then 
                    (select ri.TeamRate 
                    from rate_info ri 
                    where ri.rateyear>= replace(bc2.putoutdate,'/','-') 
                    and ri.teamtime ='1' 
                    and rownum=1 
                    order by ri.rateyear asc group by ri.rateyear )        
                else 0  end 
              ) businessrate 
            from  bc2, 
                lc2 
              where bc2.nomb = lc2.nomb 
              and  bc2.nomb not in(select bd2.no2 from  bd2) 
              and bc2.putdate is not null 
              and bc2.matdate is not null; 
      

  5.   

    select ri.TeamRate 
                    from rate_info ri 
                    where ri.rateyear>= replace(bc2.putoutdate,'/','-') 
                    and ri.teamtime ='1' 
                    and rownum=1 
                    order by ri.rateyear asc 此句查询得到的不是最大值吧
    select teamrate
    from(
    select ri.TeamRate 
                    from rate_info ri 
                    where ri.rateyear>= replace(bc2.putoutdate,'/','-') 
                    and ri.teamtime ='1' 
                    order by ri.rateyear asc

    where  rownum=1 这样才是对的