日期:2014-05-18 浏览次数:20561 次
Declare @A Table([ID] Int, [Time] DateTime,[count] int) InSert Into @A Select 1, '2012-3-5 17:30:20', 100 Union Select 2, '2012-3-5 13:30:20', 135 Union Select 2, '2012-4-6 9:30:20', 400 Union Select 3, '2012-5-7 20:30:20', 100 --1. 根据月份查询总数量(比如,3月份的总数量就是235) Select Month([Time]) 月份, Sum(Count) 总数量 From @A Where Month([Time]) = 3 Group By Month([Time]) --2. 根据月份查询增量(比如:4月份较3月份的增量就是165) Select Cast(T1.月份 As Varchar(10))+'月份较'+Cast(T2.月份 As Varchar(10))+'月份', T1.总数量 - T2.总数量 From (Select Month([Time]) 月份, Sum(Count) 总数量 From @A Group By Month([Time])) T1, (Select Month([Time]) 月份, Sum(Count) 总数量 From @A Group By Month([Time])) T2 Where T1.月份 = T2.月份 + 1 --3. 根据每周查询总数量 Select DateName(ww,[Time]) 周, Sum(Count) 总数量 From @A Where DateName(ww,[Time]) = 14 Group By DateName(ww,[Time]) --4. 根据每周查询增量 Select Cast(T1.周 As Varchar(10))+'周较'+Cast(T2.周 As Varchar(10))+'周', T1.总数量 - T2.总数量 From (Select DateName(ww,[Time]) 周, Sum(Count) 总数量 From @A Group By DateName(ww,[Time])) T1, (Select DateName(ww,[Time]) 周, Sum(Count) 总数量 From @A Group By DateName(ww,[Time])) T2 Where T1.周 < T2.周