我有一个表
年月     货物数量  
2011-1-1    98
2011-1-25   10 
2011-2-3    13
2011-3-5    4我想显示成为年月    货物数量
2011-1     0
2011-2     108
2011-3     121
2011-4     125意思就是2011-1月份统计的是1月份之前的所有货物数量,2011-2月份统计的是2月份之前的所有货物数量,以此类推,请问下这句SQL语句该怎么写啊

解决方案 »

  1.   


    select '2011-01' as date,
        (select sum(quantity) from tb where convert(varchar(6),date,112)<='201101') as qty
    from tb
      

  2.   

     年月 货物数量   
    2011-1-1 98
    --2011-1-25 10  
    --2011-2-3 13
    --2011-3-5 4--我想显示成为--年月 货物数量
    --2011-1 0
    --2011-2 108
    --2011-3 121
    --2011-4 125
     
     
    if OBJECT_ID('tb')is not null
    drop table tb
    go
    create table tb(年月 varchar(50),货物数量 int)
    insert into tb select '2011-1-1',98 union all
    select '2011-1-25',10 union all
    select '2011-2-3',13 union all
    select '2011-3-5',4  union all
    select '2011-4-5',26   
     
    select cast(datepart(yy,年月) as varchar(50))+'-'+ cast(datepart(mm,年月) as varchar(50) )as 年月
     ,(select sum(货物数量) from tb where datepart(mm,t.年月)>datepart(mm,年月))as 货物数量 from tb t年月                                                                                                    货物数量
    ----------------------------------------------------------------------------------------------------- -----------
    2011-1                                                                                                NULL
    2011-1                                                                                                NULL
    2011-2                                                                                                108
    2011-3                                                                                                121
    2011-4                                                                                                125(5 行受影响)
      

  3.   

    select distinct cast(datepart(yy,年月) as varchar(50))+'-'+ cast(datepart(mm,年月) as varchar(50) )as 年月
     ,(select sum(货物数量) from tb where datepart(mm,t.年月)>datepart(mm,年月))as 货物数量 from tb t年月                                                                                                    货物数量
    ----------------------------------------------------------------------------------------------------- -----------
    2011-1                                                                                                NULL
    2011-2                                                                                                108
    2011-3                                                                                                121
    2011-4                                                                                                125(4 行受影响)
      

  4.   


    create table t346
    (年月 date, 货物数量 int)insert into t346
    select '2011-1-1', 98 union all
    select '2011-1-25', 10 union all  
    select '2011-2-3', 13 union all
    select '2011-3-5', 4
    select a.年月,
    (select isnull(sum(货物数量),0) 
    from t346 b where b.年月<convert(date,a.年月+'-01')) 货物数量
    from 
    (
    select distinct left(convert(varchar(12),年月,23),7) 年月 from t346
    union all
    select left(convert(varchar(12),dateadd(m,1,max(年月)),23),7) from t346
    ) a年月           货物数量
    ------------ -----------
    2011-01        0
    2011-02        108
    2011-03        121
    2011-04        125(4 row(s) affected)
      

  5.   

     select  年月, isnull(sum(货物数量),0) 货物数量 from
    (
       select cast(year(年月) as varchar(50))+'-'+ cast(month(年月) as varchar(50) )as 年月
       ,(select sum(货物数量) from tb where year(年月)>month(年月))as 货物数量 from tb t
      
     ) tbl group by 年月