select identity(int,1,1) as No,* into #T from yourtable
    select *,(select sum(本月用量) as 总计 from 
        #t where No<=tem.No and convert(char(4),年月,120)='2002' as 本年本本月止累计用量 from #t as tem

解决方案 »

  1.   

    select 年月,车间名称,化学品名称,本月用量,
        (select sum(本月用量) from table_name where year(年月)=year(t1.年月) and
        month(年月)<=month(t1.month)) as '本年本月止累计用量'
    from table_name as t1
      

  2.   

    select 年月,车间名称,化学品名称,本月用量,
        (select cast(sum(cast(left(rtrim(ltrim(本月用量)),len(rtrim(ltrim(本月用量)))-1) as float))as varchar(10))+'吨' from table_name where year(年月)=year(t1.年月) and month(年月)<=month(t1.年月)) as '本年本月止累计用量'
    from table_name as t1