Link to home
Start Free TrialLog in
Avatar of Amour22015
Amour22015

asked on

SQL Server 2014 - Returning a Percentage of 2 Summed Values

Hi Experts,

I have this part of a calculation:
Cast(ISNULL(Sum(ResolvedVul60)/NULLIF(Sum(TotalVul), 0) * 100, 0) as Decimal(18,2)) as [Percent 60]

Open in new window


but the outcome Example:
TotalVul = 64
ResolvedVul60 = 5

I get:
0.00


Please help and thanks...
SOLUTION
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tested trial -

/*------------------------
SELECT Cast(ISNULL(Sum(5) * 1. / NULLIF(Sum(64), 0) * 100, 0) as Decimal(18,2)) as [Percent 60]
------------------------*/
Percent 60
---------------------------------------
7.81

(1 row(s) affected)

Open in new window

or this -

Also note that the numerator should be numeric.

/*------------------------
SELECT Cast(ISNULL(  CAST ( Sum(5) AS decimal(18,9)) / NULLIF(Sum(64), 0) * 100, 0) as Decimal(18,2)) as [Percent 60]
------------------------*/
Percent 60
---------------------------------------
7.81

(1 row(s) affected)

Open in new window

Which is correct due to the INTEGER division.. the question is: What result do you expect?

User generated image
Avatar of Amour22015
Amour22015

ASKER

Thanks for helping...
Welcome !!