April 2, 2010 at 9:15 am
I have a query (SQL 2000) which works fine:
Select
sum(case when a.[Month] = 1 then ([Avg Balance]) else 0 end) as [Jan Avg Bal] ,
sum(case when a.[Month] = 2 then ([Avg Balance]) else 0 end) as [Feb Avg Bal] ,
sum(case when a.[Month] = 3 then ([Avg Balance]) else 0 end) as [Mar Avg Bal] ,
sum(case when a.[Month] = 4 then ([Avg Balance]) else 0 end) as [Apr Avg Bal] ,
sum(case when a.[Month] = 5 then ([Avg Balance]) else 0 end) as [May Avg Bal] ,
sum(case when a.[Month] = 6 then ([Avg Balance]) else 0 end) as [Jun Avg Bal] ,
sum(case when a.[Month] = 7 then ([Avg Balance]) else 0 end) as [July Avg Bal] ,
sum(case when a.[Month] = 8 then ([Avg Balance]) else 0 end) as [Aug Avg Bal] ,
sum(case when a.[Month] = 9 then ([Avg Balance]) else 0 end) as [Sep Avg Bal] ,
sum(case when a.[Month] = 10 then ([Avg Balance]) else 0 end) as [Oct Avg Bal] ,
sum(case when a.[Month] = 11 then ([Avg Balance]) else 0 end) as [Nov Avg Bal] ,
sum(case when a.[Month] = 12 then ([Avg Balance]) else 0 end) as [Dec Avg Bal]
FROM tble a
GROUP BY a.[xxx], a.[yyyy]
Output Columns are JAN Feb, ...DEC regardless of year. The actual results are fine, just the columns
should go from lowest to highest Month/Date i.e DEC 2009 Jan 2010 Feb 2010 not Jab 2010 Feb 2010 Dec 2009
I need to output with lowest date (Dec 2009) to highest date (Feb 2010) :
Is that possible ?
Any help or pointers would be appreciated as I am not a sql programmer
April 2, 2010 at 12:50 pm
newmang, I don't see anywhere you are telling the query which years to pull. Is there a collumn for year? If so, you might want to add 'AND a.year = 2009' to the December row, and 'AND a.year = 2010' for the rest of the months. As for the order of the months, they will appear in the order you specify in your query. So, if you want December to be first, then select it first.
Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
April 2, 2010 at 1:06 pm
i do have a [Year] attribute, so I will give this a shot. I did something like this, but didn't specify the actual year value.
Thanks!@
April 2, 2010 at 8:59 pm
You'll need a bit of dynamic SQL to do what you want. Just so happens, I know a place that has an example...
http://www.sqlservercentral.com/articles/Crosstab/65048/
--Jeff Moden
Change is inevitable... Change for the better is not.
April 2, 2010 at 9:50 pm
April 2, 2010 at 10:31 pm
Garadin (4/2/2010)
I've read several of that guys articles, good stuff. 😉
Heh... thanks, Seth. Good finish to a long day.:-)
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply