有一张客户开销户表(groupinfo)有如下字段:
netcode 网点号
groupid 客户编号
opensign 开销户标志(0=开户 1=销户)
transtime 开销户日期同一个客户(groupid)可能存在几条开销户记录。
现在要把销户记录中的网点号改成最后一次开户的网点号,SQL应该怎么写?
最好效率能高一点的。

解决方案 »

  1.   


    select a.netcode
    from groupinfo a
    where not exists(
          select 1
          from groupinfo b
          where a.groupid=b.groupid
            and a.transtime<b.transtiem
          );
      

  2.   

    update groupinfo set netcode =( 
    select netcode from groupinfo where opensign=0  and transtime =(
    select max(transtime) from groupinfo where opensign=0 group by groupid)
    ) where opensign=1;
      

  3.   

    update groupinfo set (groupid,netcode) =( 
    select groupid,netcode from groupinfo b where b.opensign=0  and b.transtime =(
    select max(transtime) from groupinfo c where c.opensign=0 and b.groupid = c.groupid  group by c.groupid)
    ) where opensign=1;
      

  4.   

    BOBO12082119的方法比yejihui9527要快一点,而且yejihui9527你的脚本写错了。