lannym
MIS
- Feb 5, 2007
- 11
Hello, I'm trying to complete a SQL Query to return a set of data for creating a dynamic chart.
For example; I would like to return the data for the last 12 months. So for Feb 2007, would return data for Jan 06 to Jan 07, and in Mar 07, data returned would be Feb 06 to Feb 07.
I'm somewhat new to SQL programming, if there is a better way to accoplish this goal, i'm open to ideas!
I'm creating this Query within another program (netcharts), which is used to create the chart and display it on a webpage.
(Another question: How can I change the date format of the returned date? I know based on what i have in the Query, it will only show the month as a number, but how would I have it show the month and the year (1/2006 or 2/2007 for example)
For example; I would like to return the data for the last 12 months. So for Feb 2007, would return data for Jan 06 to Jan 07, and in Mar 07, data returned would be Feb 06 to Feb 07.
I'm somewhat new to SQL programming, if there is a better way to accoplish this goal, i'm open to ideas!
I'm creating this Query within another program (netcharts), which is used to create the chart and display it on a webpage.
(Another question: How can I change the date format of the returned date? I know based on what i have in the Query, it will only show the month as a number, but how would I have it show the month and the year (1/2006 or 2/2007 for example)
Code:
SELECT month([SQLTranDate]) as Month, sum([SQLTranAmt]) as Total
FROM chart1
/* Show Last year and current year data - 12 month only based on current month */
Where (year([SQLTranDate]) = year(getdate()) -2 and month([SQLTranDate]) <= month(getdate()))
OR
(year([SQLTranDate]) = year(getdate()) -1 and month([SQLTranDate]) > month(getdate()))
GROUP BY month([SQLTranDate])
ORDER BY month([SQLTranDate])