I’m unable to take the 12 months back from current date in a SQL statement to use with my Stimulsoft report. The sql table structure is this (figure 1), note that im brazilian so the culture is set to pt-BR (dd/mm/yyyy)
this is the SQL table
http://img534.imageshack.us/img534/1093/clipboard02xva.jpg
I want to make this, the current date for the last 12 months is 01/02/2013 so we take:
| DACP_Id | FEB 2012 | MAR 2012 | ABR 2012 ... | FEB 2013
… and all the months heading back to february 2013. The rows inside the months in the pivot table are the DACP_Value in the current month.
I’m new to SQL so I hope you can help 🙂 Thanks.
I would be inclined not to use a dynamic pivot, and to handle the headers in your application code:
If you really need the Columns headers as they are then use:
You can change the value
112when converting a date to a varchar to various numbers to change the format of your date in the column headers (103 will give you dd/mm/yyyy as stated in the question).SQL Fiddle (Shamelessly stolen from @bluefeet, who beat me to the answer). I’ve left this answer here though as it is slightly different as it will provide 13 columns (for the last year and current month) whether or not data exists for that month in the table.