i need sql query for report from table item per day(fix day1-day31 as column) of the month when i input month and year.
This is my table (item)
ID | NAME | DATE
---------------------------------------------------
1 | ITEM A | 2015-2-25 13:37:49
2 | ITEM A | 2015-2-25 14:37:49
3 | ITEM A | 2015-2-26 13:30:55
4 | ITEM B | 2015-2-26 15:37:49
5 | ITEM B | 2015-2-26 17:57:49
6 | ITEM C | 2015-2-27 13:00:33
(input month=02 and year=2015)
What I need to achieve with a view is the following:
NAME | 1| 2| 3|…|25|26|27|28|29|30|31|Total
------------------------------------------------------
ITEM A| 0| 0| 0|…| 2 | 1 | 0 | 0 | 0 | 0 | 0 | 3
ITEM B| 0| 0| 0|…| 0 | 2 | 0 | 0 | 0 | 0 | 0 | 2
ITEM C| 0| 0| 0|…| 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1
Any ideas would be very much appreciated.
Thanks in advance.
Sorry this is my first post.
This will do it for you. First test data:
Then the query. Note you can use any data range, so if you want a full month just calculate that and put it in the @startDate and @endDate
Output is:
You can determine the date of each column by parsing the column header in your presentation code (it's format is colYYYYMMDD).
You can do this using a PIVOT in your query