Wednesday, March 7, 2012

Expressing Values as Percentages

I have two values and I want to express a third derived value as a
percentage of the other two values. I thought it would be a simple
division of the first two numbers and then a multiplication by 100 to
give me a percentage, but all I get is 0.

Here is my select statement,

SELECT dbo.Eligble.GRADETotal,
dbo.nil1234_Faculties_Totals.FACTotal,
dbo.nil1234_Faculties_Totals.FACTotal /
dbo.Eligble.GRADETotal * 100 AS [PERCENT]
FROM dbo.Eligble CROSS JOIN
dbo.nil1234_Faculties_Totals

Can anyone point out where I'm going wrong here?

Thanks in advance"Bryan" <bmcguire@.rics.org.uk> wrote in message
news:99dcdbd.0309081104.359f931a@.posting.google.co m...
> I have two values and I want to express a third derived value as a
> percentage of the other two values. I thought it would be a simple
> division of the first two numbers and then a multiplication by 100 to
> give me a percentage, but all I get is 0.
>
> Here is my select statement,
> SELECT dbo.Eligble.GRADETotal,
> dbo.nil1234_Faculties_Totals.FACTotal,
> dbo.nil1234_Faculties_Totals.FACTotal /
> dbo.Eligble.GRADETotal * 100 AS [PERCENT]
> FROM dbo.Eligble CROSS JOIN
> dbo.nil1234_Faculties_Totals
>
> Can anyone point out where I'm going wrong here?
> Thanks in advance

It would be useful to see your table DDL (CREATE TABLE statements), but the
most likely reason is that you're dividing two integers. Dividing integers
always gives an integer result:

select 6 / 10 -- gives 0

select 6.0 / 10.0 -- gives 0.6

In your case, you could use CAST() or CONVERT() to change the values to
decimal with a suitable precision/scale for your calculations, eg.:

(cast(FACTotal as decimal(10,4)) / cast(GRADETotal as decimal(10,4)))* 100
AS [PERCENT]

Simon|||"Simon Hayes" <sql@.hayes.ch> wrote in message news:<3f5cd50c_3@.news.bluewin.ch>...
> "Bryan" <bmcguire@.rics.org.uk> wrote in message
> news:99dcdbd.0309081104.359f931a@.posting.google.co m...
> > I have two values and I want to express a third derived value as a
> > percentage of the other two values. I thought it would be a simple
> > division of the first two numbers and then a multiplication by 100 to
> > give me a percentage, but all I get is 0.
> > Here is my select statement,
> > SELECT dbo.Eligble.GRADETotal,
> > dbo.nil1234_Faculties_Totals.FACTotal,
> > dbo.nil1234_Faculties_Totals.FACTotal /
> > dbo.Eligble.GRADETotal * 100 AS [PERCENT]
> > FROM dbo.Eligble CROSS JOIN
> > dbo.nil1234_Faculties_Totals
> > Can anyone point out where I'm going wrong here?
> > Thanks in advance
> It would be useful to see your table DDL (CREATE TABLE statements), but the
> most likely reason is that you're dividing two integers. Dividing integers
> always gives an integer result:
> select 6 / 10 -- gives 0
> select 6.0 / 10.0 -- gives 0.6
> In your case, you could use CAST() or CONVERT() to change the values to
> decimal with a suitable precision/scale for your calculations, eg.:
> (cast(FACTotal as decimal(10,4)) / cast(GRADETotal as decimal(10,4)))* 100
> AS [PERCENT]
> Simon

Simon,

Thanks for the response. Your solution worked a treat :)

thanks again

Bryan

No comments:

Post a Comment