select *
       ,(select sum(工程量) 
           from T 
          where 细目编号 = a.细目编号 
                and 项目名称 = a.项目名称
                and 年份 = a.年份
                and 月份 < a.月份
         )  
       ,(select sum(工作量) 
           from T 
          where 细目编号 = a.细目编号 
                and 项目名称 = a.项目名称
                and 年份 = a.年份
                and 月份 < a.月份
         ) 
from T a

解决方案 »

  1.   

    select a.* ,b.完成工程量,c.完成工作量 from t_1left join (select sum(工程量) as 完成工程量 from t_1) t_2
                   on  t_1.细目编号 = t_2.细目编号   and t_1.年份=t_2.年份
                    and t_2.月份< t_1.月份left join (select sum(工作量) as 完成工作量 from t_1) t_3
                   on  t_1.细目编号 = t_2.细目编号   and t_1.年份=t_2.年份
                                     and t_3.月份< t_1.月份
      

  2.   

    declare @给定的年份 int,@给定的月份 int select *
           ,(select sum(工程量) 
               from T 
              where  (年份 < @给定的年份) or                (年份 = @给定的年份 and 月份 < @给定的月份)) as 完成工程量  
           ,(select sum(工作量) 
               from T 
              where  (年份 < @给定的年份) or                (年份 = @给定的年份 and 月份 < @给定的月份)) as 完成工作量    
    from T 
    where 年份 = @给定的年份 and 月份 = @给定的月份
    给变量赋上你的值就可以了