呵呵,分很多。可是没有本事拿啊。呵呵~~~帮你顶一下。

解决方案 »

  1.   

    太麻烦了,想想头都大了,就算能写出来,也要花一天时间
      

  2.   

    哇,这么复杂,要写出来起码都要半天时间了
      

  3.   

    create table #你的表(year int,code int,dept int,cus varchar(10),fs int,me int)
    insert #你的表 values(2001,    1131    , 112,      'AS11'   ,  25,      30)
    insert #你的表 values(2002 ,   1131   ,  112 ,     'AS11'  ,   60 ,     70)
    insert #你的表 values(2003  ,  1131  ,   112  ,    'AS11' ,    40  ,    110)
    insert #你的表 values(2002   , 1131 ,    044   ,   'SDF' ,     50    ,  70)
    insert #你的表 values(2003    ,1131,     044    ,  'SDF',      40   ,   20)
    select a.cus,a.me,case when me<=fs then me else fs end firstyear case when a.me<=a.fs then 0 when a.me-a.fs<=b.fs then a.me-a.fs else b.fs end secondyear,case when a.me<=a.fs then 0 when b.me-b.fs<=c.fs then b.me-b.fs else b.fs end thirdyear,case when a.me<=a.fs then 0 when b.me-b.fs<=c.fs then b.me-b.fs else b.fs end secondyear from #你的表 a left join #你的表 b on a.year=b.year+1 and a.cus=b.cus left join #你的表 c on a.year=c.year+2 and a.cus=c.cus where a.[year]=(select max([year]) from #你的表 where cus=a.cus)go
    drop table #你的表