SQL2005,
就是A表是现在也就是3月的数据,如果B表里面有字段不为0,就用A表中的数据减去B表对应字段,
因为B表上面是2月的,那么就在2月的前面月显示减过的数据,也就是1月;
打个比方来说,如果现在是5月,B表数据是4月
得到的结果就是
A01,0,2,1,3,8,2014,1
A01,0,2,1,3,8,2014,2
A01,0,2,1,3,8,2014,3
A01,0,2,3,3,8,2014,4
A01,0,2,3,3,8,2014,5

.

A01,0,2,3,3,8,2014,12

解决方案 »

  1.   

    理解得不一定对吭
    select a.dptid,a.age1-isnull(b.age1,0),a.age2-isnull(b.age2,0),a.age3-isnull(b.age3,0),a.age4-isnull(b.age4,0),a.nowqty,a.year,a.number as [Month] from 
    (select A.*,number from A,master..spt_values where number>=1 and number<=12 and type='P') a left join
    (select B.*,number from B ,master..spt_values where number>=1 and number<B.mon and type='P') b
    on a.dptid=b.dptid and a.number=b.number
    order by a.dptid,a.number