这在sql中是否可行?
我有一个包含以下行的表
StoreID | Date | SalesItem |
1 | 2016-08-16 | Book |
2 | 2016-08-16 | Pen |
1 | 2016-08-15 | Pen |
1 | 2016-08-15 | Book |
我想要的结果是
Store | Week | Sales
1 | 11 | 30
2 | 11 | 15
我正在使用sql server 2008,数据集比上面的例子大得多,但这基本上是我想要在sql中实现的,而不是在PHP之后处理。
到目前为止我所拥有的是
select [DATE], [store], count(store) as total, DATEPART(ww,DATE)
AS weeknum from [contracts] where [DATE] >= DATEADD(month, -12, GetDate())
group by [DATE], [store] order by [DATE] asc
答案 0 :(得分:2)
尝试以下代码
SELECT Store,DATEPART(WW,Date),SUM(Sales) FROM TABLE1
GROUP BY Store, DATEPART(WW,Date)
根据问题编辑
SELECT Store,DATEPART(WW,Date),count(Sales) FROM TABLE1
GROUP BY Store, DATEPART(WW,Date)