操作方法:计算一年中每7天销售一次的小部件

时间:2011-03-03 20:51:11

标签: sql tsql stored-procedures

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.CreateTS, 101)AS  SMALLDATETIME), FT.FormName

当前的代码汇总了每天销售的小部件数量,并且可以追溯到第1天。我需要知道每7天售出多少。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:2)

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