August 8, 2006 at 4:18 pm
I'm doing a calculation in a select from which I need a double returned:
SELECT SUM( RiderCount)/Count(*) as RiderAvg
but RiderAvg is returned as a whole number. Is there a function or other method I can use to get this result?
Thanks much.
August 8, 2006 at 4:43 pm
SELECT SUM(CONVERT(double, RiderCount)) / count(*) as RiderAvg
August 8, 2006 at 7:10 pm
Thanks, that's what I was looking for!
August 10, 2006 at 2:48 pm
Why do a CONVERT on every row? CONVERT the SUM instead:
SELECT
CONVERT(double, SUM(RiderCount)) / count(*) as RiderAvg
August 10, 2006 at 3:14 pm
Thanks for the optimization. How about using CAST instead of CONVERT, is there preference for one over the other?
Thanks.
August 10, 2006 at 4:36 pm
I am not aware of any performance difference. I find CAST more readable, but CONVERT provides an extra Style parameter to control formatting. Use whichever one makes you happy.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply