Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using Percentages

Author  Topic 

WelshPunk
Yak Posting Veteran

67 Posts

Posted - 2008-05-01 : 01:29:27
The formula for percentage safe = sum(safe)/ sum(safe)+sum(unsafe)*100.0

This works well on a calculator. Could someone show me how to achieve this in sql please. If we use safe = 92 and unsafe = 117

This forum is great!

Regards Phill

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-01 : 01:42:53
SELECT PercentSafe=SUM(Safe)* 100.0 /(Sum(Safe) + Sum(unsafe)) From YourTable
Go to Top of Page

WelshPunk
Yak Posting Veteran

67 Posts

Posted - 2008-05-01 : 03:41:37
Brill thanks for that. Is there a way of rounding the output to 2 decimal place
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2008-05-01 : 03:55:26
select PercentSafe=convert(decimal(18,2),SUM(Safe)* 100.0 /(Sum(Safe) + Sum(unsafe))) From YourTable
Go to Top of Page

WelshPunk
Yak Posting Veteran

67 Posts

Posted - 2008-05-01 : 05:11:00
Thank you
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-02 : 06:07:44
quote:
Originally posted by WelshPunk

The formula for percentage safe = sum(safe)/ sum(safe)+sum(unsafe)*100.0

This works well on a calculator. Could someone show me how to achieve this in sql please. If we use safe = 92 and unsafe = 117

This forum is great!

Regards Phill


http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -