2011-03-03 121 views
1
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天销售多少。如何:每7天销售一次,每年销售小部件

任何帮助都会很棒。

回答

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