Monday, February 20, 2012

Rating System / AVG (Easy Question)

I am building a rating system, and there will be lots of records that will
simply be 1,2,3,4 or 5. tinyInt obviously works fine for this, BUT:
I want to do something like 'SELECT AVG(ratings) as avgRating' And this
always returns an tinyint datatype since the ratings are tinyints. I want
slightly more precision than a tinyint for the average, what I want is
something in the format of '#.##' (i.e. 3.45). What datatypes do I need to
store the ratings in and what command can I use to get JUST a #.##?
I've changed the datatypes to money and decimal and used the ROUND function,
but it still returns something like '3.4500'.
Any help is appreciated, thanks in advance
CraigThis is a multi-part message in MIME format.
--=_NextPart_000_002A_01C36729.1E7833E0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Try:
select avg (cast (rating as numeric (3, 2)))
from MyTable
--
Tom
"Craig S" <craig@.removethis_birch.net> wrote in message =news:eVy6Pe0ZDHA.3768@.tk2msftngp13.phx.gbl...
> I am building a rating system, and there will be lots of records that =will
> simply be 1,2,3,4 or 5. tinyInt obviously works fine for this, BUT:
> > I want to do something like 'SELECT AVG(ratings) as avgRating' And =this
> always returns an tinyint datatype since the ratings are tinyints. I =want
> slightly more precision than a tinyint for the average, what I want is
> something in the format of '#.##' (i.e. 3.45). What datatypes do I =need to
> store the ratings in and what command can I use to get JUST a #.##?
> > I've changed the datatypes to money and decimal and used the ROUND =function,
> but it still returns something like '3.4500'.
> > Any help is appreciated, thanks in advance
> Craig
> > --=_NextPart_000_002A_01C36729.1E7833E0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Try:
select avg (cast (rating as =numeric (3, 2)))
from MyTable
Tom
"Craig S" wrote in message news:eVy6Pe0ZDHA.3768@.tk2msftngp13.phx.gbl...> I =am building a rating system, and there will be lots of records that will> =simply be 1,2,3,4 or 5. tinyInt obviously works fine for this, BUT:> => I want to do something like 'SELECT AVG(ratings) as avgRating' And =this> always returns an tinyint datatype since the ratings are tinyints. I want> slightly more precision than a tinyint =for the average, what I want is> something in the format of '#.##' (i.e. 3.45). What datatypes do I need to> store the ratings in =and what command can I use to get JUST a #.##?> > I've changed the datatypes to money and decimal and used the ROUND function,> but =it still returns something like '3.4500'.> > Any help is =appreciated, thanks in advance> Craig> >

--=_NextPart_000_002A_01C36729.1E7833E0--

No comments:

Post a Comment