computation for average is not correct (imagine there can be more than 2 transactions a day for each account) and instead of join, a union all will doto get you started...declare @FACT table(dt datetime,account_no varchar(6),amount int,eom varchar(1))insert into @factselect '1/2/2006','080001','1000','N' union select'1/2/2006','080002','500','N' union select'1/2/2006','080003','5000','N' union select'1/3/2006','080001','2000','N' union select'1/3/2006','080002','1000','N' union select'1/3/2006','080003','1500','N' union select'1/4/2006','080001','2000','N' union select'1/4/2006','080002','1000','N' union select'1/4/2006','080003','1500','N' union select'1/31/2006','080001','7000','Y' union select'1/31/2006','080002','5000','Y' union select'1/31/2006','080003','8000','Y'select (datename(month,dt) + ' ' + convert(varchar(4),datepart(year,dt))) as month_year,'end of month balance' as balance_type,account_no,sum(amount)/31 as amount from @factgroup by account_no,eom,datename(month,dt) + ' ' + convert(varchar(4),datepart(year,dt))having eom='N'union allselect (datename(month,dt) + ' ' + convert(varchar(4),datepart(year,dt))) as month_year,'end of month balance' as balance_type,account_no,amountfrom @fact where eom='Y'
--editor did you mean sum(amount)/count(*) ?--------------------keeping it simple...