Do it (or at least think about it) in two steps. In the first step, get the data aggregated by month. So you would do something like this:select AppId, Datename(month,logdate) as Mnth, Year(logdate) as Yr, sum(dailycount) as MnthTotalfrom DailyTransactionCountgroup by AppId, Datename(month,logdate), Year(logdate)
This now gets you the data, but it is not in the correct form that you are looking for. So the second step would be pivoting this data to get it in the form you want.If you know in advance how many applications you will have, then you can use SQL 2005's built in PIVOT. If you don't, you will need to use dynamic pivoting - see here:http://www.sqlteam.com/article/dynamic-cross-tabs-pivot-tables or http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspxIf you are able to use the "static" pivot, it would be like this:with CTE as( -- insert the previous select that I showed above here)select *from CTEPIVOT ( sum(MnthTotal) for AppId in([AppId1],[AppId2],[AppId3])) P -- and may be more AppIds