我有每日级别的销售数据,我想将其聚合到SQL服务器中的每周级别。
任何一周的开始时间应分别为星期二和星期一为星期一
请帮忙
答案 0 :(得分:0)
您应该发布一些关于您尝试过的内容。也许给我们一些示例代码和您遇到的问题。话虽如此,请参阅下文。在没有看到您的解决方案细节的情况下,有很多不同的方法。
SET DATEFIRST 2;
DECLARE @SalesData TABLE (ID INT PRIMARY KEY IDENTITY(1,1), SalesDate DATETIME, Price MONEY)
INSERT INTO @SalesData (SalesDate,Price) VALUES
('1/1/2013',12.00),
('1/2/2013',14.00),
('1/3/2013',11.28),
('5/3/2013',11.64),
('5/3/2013',18.00)
;WITH
SalesInfo AS
(
SELECT sd.id,
YEAR(sd.SalesDate) AS TheYear,
DATEPART(week,sd.SalesDate) AS TheWeek
FROM @SalesData sd
)
SELECT SI.TheYear,
SI.TheWeek,
SUM(SD.Price) AS TotalForWeek
FROM @SalesData AS SD
INNER JOIN SalesInfo AS SI ON (SD.ID = SI.ID)
GROUP BY SI.TheYear,SI.TheWeek
SET DATEFIRST 7