How To: Count widgets sold every 7 days for a period of a year
SELECT
CAST(CONVERT(varchar, W.CreateTS, 101)AS SMALLDATETIME) AS [SoldDate]
,COUNT(*) AS NumberOfWidgets
,FT.FormName
FROM tblWidget W
JOIN tblFormType FT ON (W.FormTypeID = FT.FormTypeID)
WHERE W.CreateTS >= DATEADD(YEAR, -1, @RunDate)
GROUP BY CAST(CONVERT(varchar, W.C开发者_Python百科reateTS, 101)AS SMALLDATETIME), FT.FormName
The current Code aggregates the amount of widgets sold per day and goes back year - 1 day. I need to find out how many are sold per 7 days.
Any help would be awesome.
SELECT
DATEPART(week, w.CreateTS) AS [SoldWeek]
,COUNT(*) AS NumberOfWidgets
,FT.FormName
FROM tblWidget W
JOIN tblFormType FT ON (W.FormTypeID = FT.FormTypeID)
WHERE W.CreateTS >= DATEADD(YEAR, -1, @RunDate)
GROUP BY DATEPART(week, w.CreateTS), FT.FormName
精彩评论