Need to perform a loop

I have a query that I need to be able to run for an entire year (once a month) and for each of my business areas (18 of them). So I'm looking for a way to generate results like the attached file (our regions are on the left - there are 18 of them) and each month of the year appears to the top of the page beginning from July (or Financial Year start) and ending in June.

Preferrably I'd like to be able to pass a parameter of the year to the query so this basically built the stats for the requested year (from July to June obviously).

I've attached the code I'm currently using to manually check each month of the year so far so this gives you an idea of what I'm looking to do. However, one of the stumbling blocks I see is that the query will return only the regions that have data. I need to be able to show if a region had 0 entries for that month (the data doesn't contain this obviously). So I'm wondering if every field needs to be 0 unless overwritten by a value from the query but as I say I'm not sure about this part.

Any advice/assistance that you can offer is appreciated.
SELECT     COUNT(FilteredAccount.new_region) AS Awards
FROM         FilteredAccount INNER JOIN
                      FilteredOpportunity ON FilteredAccount.accountid = FilteredOpportunity.accountid
WHERE     ((FilteredOpportunity.new_fundsreleaseddate >= '01/01/2009') AND (FilteredOpportunity.new_fundsreleaseddate <= '01/31/2009')) AND ((FilteredOpportunity.new_producttype <> 6) OR ((FilteredOpportunity.new_startupflag = 1) AND ((FilteredOpportunity.new_startupdate >= '01/01/2009') AND (FilteredOpportunity.new_startupdate <='01/31/2009'))) )
GROUP BY FilteredAccount.new_region
ORDER BY FilteredAccount.new_region

Open in new window

AwardsTrialforEE.xls
LVL 2
Steven O'NeillSolutions ArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RiteshShahCommented:
well, you are looking for PIVOT, have a look at my blog at below link.

http://www.sqlhub.com/search?q=pivot

first article will be difficult so you can skip it and can look at other articles.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RiteshShahCommented:
0
Steven O'NeillSolutions ArchitectAuthor Commented:
Thanx for this. PIVOT is the way to go to do this but I'm having a few difficulties getting this sorted but at least I'm onto the right direction now.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.