x

adding a percent

I am trying to get a percent from 2 fields ie add [A]+[B]/[A]*100 AS [C] to get my percent. if i try [A]/sum([A]+[B])*100 it returns a 0 and if i try sum([A]+[B])/[A]*100 i get a diide by zero error. values are [A] 3 [b] 1 percent should be 75. Thanks for any help on this
more ▼

asked May 29, 2012 at 11:10 AM in Default

n3w2sql gravatar image

n3w2sql
850 14 22 33

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

It's because this is doing integer division, so 3/sum(3+1) = 0

You need to convert the top line into a decimal, such as (3 * 1.0)/sum(3+1) = 0.75

This makes your query (([A]*1.0)/([A]+[B]) * 100)
more ▼

answered May 29, 2012 at 11:15 AM

Kev Riley gravatar image

Kev Riley ♦♦
52.8k 47 49 76

Thanks Kev that works perfect, I have really been struggling with percentages but I have a much better understanding now with your help. cheers
May 30, 2012 at 09:03 AM n3w2sql
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1834

asked: May 29, 2012 at 11:10 AM

Seen: 303 times

Last Updated: May 30, 2012 at 09:05 AM