I want to select data from following table group by weeks
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
Let suppose today is 30-05-11
So my result should look like this.
Product Name First Week Second Week Third Week
+--------------------+------------+------------+-------------+
a 12 6 2
b 0 0 4
c 0 3 0
Will some body guide me to how to write SQL query to achieve this behavior!
OUTPUT LOOK LIKE THIS
the provided solutions seem a little complex? this might help:
https://msdn.microsoft.com/en-us/library/ms174420.aspx
This should do it for you:
It will calculate the week number relative to the month. So instead of week 20 for the year it will be week 2. The
@DatePeriod
variable is used to fetch only rows relative to the month (in this example only for the month of May)Output using my sample data:
I think this should do it..