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

avatar image

900 22 32 39

(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

avatar image

Kev Riley ♦♦
66.8k 48 65 81

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.

Follow this question

By Email:

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



Answers and Comments

SQL Server Central

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



asked: May 29, 2012 at 11:10 AM

Seen: 673 times

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

Copyright 2018 Redgate Software. Privacy Policy