hmm...your posted code is a bit confusing. i cant see whats initial value of j. also whats AvgPrice j/TotQty?it would be better if you can post some sample data and explain what you want as output------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/