我想从下面的表格组中选择几周的数据
Date Product Name Sale +----------+--------------+-----+ 14-05-11 a 2 14-05-11 b 4 17-05-11 c 3 19-05-11 a 6 24-05-11 a 6 29-05-11 a 6
假设今天是30-05-11
所以我的结果应该是这样的.
Product Name First Week Second Week Third Week +--------------------+------------+------------+-------------+ a 12 6 2 b 0 0 4 c 0 3 0
会有一些身体引导我如何写SQL查询来实现这个行为!
解决方法
我认为这应该做
Select ProductName,WeekNumber,sum(sale) from ( SELECT ProductName,DATEDIFF(week,'2011-05-30',date) AS WeekNumber,sale FROM table ) GROUP BY ProductName,WeekNumber