select wp,sum(xsl) from yourtable where rq<getdate() and rq>dateadd(mm,-3,getdate()) group by wp

解决方案 »

  1.   

    DATEADD
    在向指定日期加上一段时间的基础上,返回新的 datetime 值。语法
    DATEADD ( datepart , number, date ) 参数
    datepart是规定应向日期的哪一部分返回新值的参数。下表列出了 Microsoft&reg; SQL Server&#8482; 识别的日期部分和缩写。日期部分 缩写 
    Year yy, yyyy 
    quarter qq, q 
    Month mm, m 
    dayofyear dy, y 
    Day dd, d 
    Week wk, ww 
    Hour hh 
    minute mi, n 
    second ss, s 
    millisecond ms 
    number是用来增加 datepart 的值。如果指定一个不是整数的值,则将废弃此值的小数部分。例如,如果为 datepart 指定 day,为 number 指定 1.75,则 date 将增加 1。date是返回 datetime 或 smalldatetime 值或日期格式字符串的表达式。有关指定日期的更多信息,请参见 datetime 和 smalldatetime。 如果您只指定年份的最后两位数字,则小于或等于"两位数年份截止期"配置选项的值的最后两位数字的数字所在世纪与截止年所在世纪相同。大于该选项的值的最后两位数字的数字所在世纪为截止年所在世纪的前一个世纪。例如,如果 two digit year cutoff 为 2049(默认),则 49 被解释为 2049,2050 被解释为 1950。为避免模糊,请使用四位数的年份。返回类型
    返回 datetime,但如果 date 参数是 smalldatetime,返回 smalldatetime。
      

  2.   

    select wp,SUM(xsl)
    FROM 表
    WHERE rq>… and rq<…
    GROUP BY wp
      

  3.   

    select wp,sum(xsl) 前3个月的销售量 from tablename 
    where rq between dateadd(mm,-3,getdate()) and getdate() group by wp
      

  4.   

    select wp,xsl,rq from yourtable where datediff(mm,rq,getdate()) and rq=...
      

  5.   

    select wp,sum(xsl) from yourtable where rq<getdate() and rq>dateadd(mm,-3,getdate()) group by wp
      

  6.   

    你说的三个月是指什么?是指最近三个月的吗?select wp,sum(xsl) from 表 where datediff(month,rq,getdate()) between 0 and 2
      

  7.   

    上面少写了group by 语句:
    select wp,sum(xsl) from 表 where datediff(month,rq,getdate()) between 0 and 2
    group by wp