先有b表  部门编号,名称,age<20,20<=age<23,23<=age<26,26<age
表中字段:dptid,dptname,age1,age2,age3,age4,qty,year,month
          01012 产品部    0     0    0    0    0   2012   1
          01012 产品部    0     1    2    0    3   2012   2
          01012 产品部    0     0    0    0    0   2012   3
          01012 产品部    0     0    0    0    0   2012   4
          01012 产品部    0     0   -1    2    1   2012   5
          01012 产品部    0     0    0    0    0   2012   6
          01012 产品部    0     0    0    0    0   2012   7
          01012 产品部    1     0    1    0    2   2012   8
          01012 产品部    2     0    1    0    3   2012   9
          01012 产品部    0    -1    0    1    0   2012   10
          01012 产品部    1     0    1    0    2   2012   11
          01012 产品部    0     0    0    0    0   2012   12
b表中有多个部门,月份是12个月,现在要把上表中3,4,6,7,12月份的值用上个月的值显示出来,如果上个月没有值,则用上上个月的值,以此类推!

解决方案 »

  1.   

    DECLARE @a TABLE(dptid CHAR(5),dptname varchar(20),age1 int,age2 int,age3 int,age4 int,qty int,[year] int,[month] int)
    INSERT @A SELECT '01012','产品部',    0,     0,    0,    0,    0,   2012,   1
    UNION ALL SELECT '01012','产品部',    0,     1,    2,    0,    3,   2012,   2
    UNION ALL SELECT '01012','产品部',    0,     0,    0,    0,    0,   2012,   3
    UNION ALL SELECT '01012','产品部',    0,     0,    0,    0,    0,   2012,   4
    UNION ALL SELECT '01012','产品部',    0,     0,   -1,    2,    1,   2012,   5
    UNION ALL SELECT '01012','产品部',    0,     0,    0,    0,    0,   2012,   6
    UNION ALL SELECT '01012','产品部',    0,     0,    0,    0,    0,   2012,   7
    UNION ALL SELECT '01012','产品部',    1,     0,    1,    0,    2,   2012,   8
    UNION ALL SELECT '01012','产品部',    2,     0,    1,    0,    3,   2012,   9
    UNION ALL SELECT '01012','产品部',    0,    -1,    0,    1,    0,   2012,   10
    UNION ALL SELECT '01012','产品部',    1,     0,    1,    0,    2,   2012,   11
    UNION ALL SELECT '01012','产品部',    0,     0,    0,    0,    0,   2012,   12
    SELECT *,QTY=
    CASE WHEN qty<>0 
    THEN qty 
    ELSE (SELECT top 1 QTY 
          FROM @A 
          WHERE dptid=a.dptid AND [year]=a.[year] AND [month]<a.[month] AND qty<>0 
          ORDER BY [month] desc 
         ) 
    END  
    FROM @A A 
    /*dptid dptname              age1        age2        age3        age4        qty         year        month       QTY         
    ----- -------------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
    01012 产品部                  0           0           0           0           0           2012        1           NULL
    01012 产品部                  0           1           2           0           3           2012        2           3
    01012 产品部                  0           0           0           0           0           2012        3           3
    01012 产品部                  0           0           0           0           0           2012        4           3
    01012 产品部                  0           0           -1          2           1           2012        5           1
    01012 产品部                  0           0           0           0           0           2012        6           1
    01012 产品部                  0           0           0           0           0           2012        7           1
    01012 产品部                  1           0           1           0           2           2012        8           2
    01012 产品部                  2           0           1           0           3           2012        9           3
    01012 产品部                  0           -1          0           1           0           2012        10          3
    01012 产品部                  1           0           1           0           2           2012        11          2
    01012 产品部                  0           0           0           0           0           2012        12          2(所影响的行数为 12 行)
    */
      

  2.   


    declare @a table(
    age1 int,
    age2 int,
    age3 int,
    age4 int,
    qty int,
    month1 int)
    insert into @a select 
       0,     0,    0,    0,    0,      1 union all select
       0,     1,    2,    0,    3,      2 union all select
       0,     0,    0,    0,    0,      3 union all select
       0,     0,    0,    0,    0,      4 union all select
       0,     0,   -1,    2,    1,      5 union all select
       0,     0,    0,    0,    0,      6 union all select
       0,     0,    0,    0,    0,      7 union all select
       1,     0,    1,    0,    2,      8 union all select
       2,     0,    1,    0,    3,      9 union all select
       0,    -1,    0,    1,    0,      10 union all select
       1,     0,    1,    0,    2,      11 union all select
       0,     0,    0,    0,    0,      12
       select * from (
    select b.age1,b.age2,b.age3,b.age4,b.qty,a.month1 from @a a,@a b 
    where a.qty=0 --这块应该是所有不为0的,我懒...就取了qty不为0的...
    and b.qty!=0 and a.month1-b.month1>0 and a.month1!=10 and not exists
    (select 1 from @a c,@a d where a.month1=c.month1 and
    c.qty=0 and d.qty!=0 and c.month1-d.month1>0 and a.month1-b.month1>c.month1-d.month1)
    union all
    select * from @a where month1 not in (3,4,6,7,12)
    )a order by month1--第一、二个还有年的字段都没加,稍微改改就行了...