表AID        Date         Fee 001    2005-11-01      100
002    2005-11-18      200
003    2005-11-28      200
004    2005-12-10      200
005    2005-12-20      100
006    2006-01-05      300
……表BMonth       AllFee2005.11     500
2005.12     300
2006.01     300
……根据表A的数据,得到表B的数据,Sql语句如何写?请指点一下……学习,关注……

解决方案 »

  1.   


    declare @t table (ID varchar(100) ,[Date] datetime,Fee int)
    insert into @t
    select '001','2005-11-01',100 union all
    select '002','2005-11-18',200 union all
    select '003','2005-11-28',200 union all
    select '004','2005-12-10',200 union all
    select '005','2005-12-20',100 union all
    select '006','2006-01-05',300
    select * from @tselect convert(varchar(7),[date],102) as [Month] ,sum(fee) as AllFee
    from @t
    group by convert(varchar(7),[date],102)
      

  2.   

    select substring(replace(builddate,'-','.'),1,7) as Month, sum(Fee) as AllFee from A
    group by substring(replace(builddate,'-','.'),1,7)
      

  3.   

    如果需要排序,还可以在最后面加上order by substring(replace(builddate,'-','.'),1,7)