根据getdate拉上周

时间:2014-06-10 16:43:41

标签: sql

我希望根据getdate总结上周的销售额。我需要查看今天的日期,这是在第24周,并将第23周的所有销售额与前一周相加。我希望基于getdate()

动态地执行此操作

1 个答案:

答案 0 :(得分:0)

以下查询将为您提供帮助

DECLARE @CurrentDate datetime,
        @CurrentWeekFrom datetime,
        @LastWeekFrom datetime,
        @LastWeekEnd datetime

SET @CurrentDate = GetDate()
SET @CurrentWeekFrom = DateAdd(wk, DateDiff(wk, 0, @CurrentDate), 0)  
SET @LastWeekFrom = DateAdd(wk, -1, @CurrentWeekFrom)
SET @LastWeekEnd = DateAdd(dd, -1, @LastWeekFrom)

SELECT SUM(Sales) AS Sales FROM table1
               Where SalesDate Between  @LastWeekFrom And @LastWeekEnd