Date Sale Product Name
+---------------------------+------------+-----------+--------------+
2018-10-25 05:27:35.9070422 1000 P1
2018-10-18 05:27:35.9070422 2000 P2
2018-10-2 05:27:35.9070422 3050 P3
2018-10-10 05:27:35.9070422 1000 P4
2018-10-5 05:27:35.9070422 1000 P5
假设今天是26-05-18
所以我的结果应该像这样。
Week Sales
+--------------------+------------+
1 4050
2 1000
3 2000
4 1000
答案 0 :(得分:1)
对于星期几,您可以使用PARTDATE()函数, 所以您的查询应该是:
select partdate(WK,r.date) numweek, sum(r.sales) totsales
from yourtable r
group by partdate(WK,r.date)
请看一下Get week number from dates in T-SQL
您还可以从partdate(WK,r.date)减去N数值,然后提防group by子句,从1开始选择星期。
希望这可以为您提供帮助
答案 1 :(得分:0)
您需要row_number()
:
select row_number() over (order by datepart(week, t.date)) as week, sum(t.amount) as sales
from table t
group by datepart(week, t.date);