OK -- so you *can* calculate the average that way but why would you?My gut feeling is that it will be no faster but that's not enough so I tested it. Data Set:IF OBJECT_ID('tempdb..#sample') IS NOT NULL DROP TABLE #sampleGOCREATE TABLE #sample ( [key] INT IDENTITY(1,1) PRIMARY KEY , [Value] MONEY )GO ; WITH power10 AS ( SELECT 1 AS [n] UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 )INSERT #sample ([value])SELECT CAST(CAST(NEWID() AS VARBINARY(4)) AS MONEY)FROM power10 AS a CROSS JOIN power10 AS b CROSS JOIN power10 AS c CROSS JOIN power10 AS d CROSS JOIN power10 AS e CROSS JOIN power10 AS f CROSS JOIN power10 AS g1 million random prices.And when I run some queries:SET STATISTICS IO ONGOSET STATISTICS TIME ONGOSELECT AVG([value]) FROM #sample-- CPU time = 1980 ms, elapsed time = 316 ms.-- CPU time = 2064 ms, elapsed time = 259 ms.-- CPU time = 2063 ms, elapsed time = 245 ms.-- CPU time = 2078 ms, elapsed time = 242 ms.-- CPU time = 1968 ms, elapsed time = 202 ms.SELECT SUM([Value]) FROM #sample-- CPU time = 2015 ms, elapsed time = 199 ms.-- CPU time = 2079 ms, elapsed time = 272 ms.-- CPU time = 1953 ms, elapsed time = 189 ms.-- CPU time = 2421 ms, elapsed time = 229 ms-- CPU time = 1702 ms, elapsed time = 268 ms..SELECT SUM([Value]) / COUNT([Value]) AS [Avg] FROM #sample-- CPU time = 1889 ms, elapsed time = 283 ms.-- CPU time = 2049 ms, elapsed time = 235 ms.-- CPU time = 2045 ms, elapsed time = 217 ms.-- CPU time = 1845 ms, elapsed time = 242 ms.-- CPU time = 1875 ms, elapsed time = 218 ms.-- Table '#sample_0000000050FC'. Scan count 25, logical reads 26018, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.SELECT SUM([value]), COUNT([value]), AVG([Value]) FROM #sample-- CPU time = 2281 ms, elapsed time = 286 ms.-- CPU time = 2296 ms, elapsed time = 265 ms.-- CPU time = 1956 ms, elapsed time = 246 ms.-- CPU time = 2013 ms, elapsed time = 248 ms.-- CPU time = 1954 ms, elapsed time = 251 ms.-- Table '#sample_0000000050FC'. Scan count 25, logical reads 26018, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
I get *completely* dominated by the IO subsystem.... completely.I could run this on a fusionIO box but.... meh....I don't think there is anything to be gained by doing the sum / count and then working out the average yourself.Maybe, just maybe, if you needed to report the sum and count anyway then you could do in via a derived table but.... I'd be surprised if there was any difference in performance.What size and shape your data?Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/