I have a table in which i have columns like :
ItemId Date CountUser CountUserPast7Days
1 20120101 20 null
1 20120102 15 null
1 20120208 12 null
2 20120209 65 null
2 20120611 32 null
2 20120615 20 null
3 20120901 45 null
I want to write an query to update CountUserPast7Days based on – group by the data on two parameters – ItmeId and Date(past 7 days) and sum the column CountUser. So the data should look like –
ItemId Date CountUser CountUserPast7Days
1 20120101 20 20
1 20120102 15 35
1 20120208 12 27
2 20120209 65 65
2 20120611 32 32
2 20120615 20 52
3 20120901 45 45
Your example output is buggy, since there is more than a month between 2012/01/02 and 2012/02/08, but I got your point all the same. What you are looking for is a self-join:
Obviously I called your table “SlidingHitCount.”