Solved

# Qry Returns an Integer on a calculation, but I want a decimal.  How do I change that?

Posted on 2009-04-22
Medium Priority
232 Views
Last Modified: 2012-05-06
Friends,

I have the following qry:

Select Count(Distinct(r.EntrantID)) as NoOfEntrants,
Sum(r.EntrantPoints) as TeamTotalPoints,
AvgPoints = (Sum(r.EntrantPoints)/Count(Distinct(r.EntrantID)))
From Results r Inner Join Entrant e
ON r.EntrantID=e.EntrantID
AND
r.SeasonID=14
AND
r.TeamID=e.TeamID
Where r.TeamID=3 and e.IncludeInAvgReport='True'
And r.SeasonID=14 and r.EntrantPoints IS NOT NULL

and it returns the following values:

4      181      45

However, it should return 45.25.  How do I change my qry to return a decimal?

Best Regards,
Eric
``````Select Count(Distinct(r.EntrantID)) as NoOfEntrants,
Sum(r.EntrantPoints) as TeamTotalPoints,
AvgPoints = (Sum(r.EntrantPoints)/Count(Distinct(r.EntrantID)))
From Results r Inner Join Entrant e
ON r.EntrantID=e.EntrantID
AND
r.SeasonID=14
AND
r.TeamID=e.TeamID
Where r.TeamID=3 and e.IncludeInAvgReport='True'
And r.SeasonID=14 and r.EntrantPoints IS NOT NULL
``````
0
Question by:indy500fan
• 2
• 2
5 Comments

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24208838
do this
``````AvgPoints = (cast( Sum(r.EntrantPoints) as decimal(20,4)) /Count(Distinct(r.EntrantID)))
``````
0

Author Comment

ID: 24208855
That'll do perfect!  Thanks!
0

LVL 60

Expert Comment

ID: 24208867
Angel is correct.  Another option is to do the following:
``````Select Count(Distinct(r.EntrantID)) as NoOfEntrants,
Sum(r.EntrantPoints) as TeamTotalPoints,
AvgPoints = (Sum(r.EntrantPoints) * 1.0/Count(Distinct(r.EntrantID)))
From Results r Inner Join Entrant e
ON r.EntrantID=e.EntrantID
AND
r.SeasonID=14
AND
r.TeamID=e.TeamID
Where r.TeamID=3 and e.IncludeInAvgReport='True'
And r.SeasonID=14 and r.EntrantPoints IS NOT NULL
``````
0

Author Comment

ID: 24208905
Any advantage between these two solutions?
0

LVL 60

Expert Comment

ID: 24208966
When I have used them, performance has been the same for me.  Both fast.
My approach does implicit conversion to floating point if I am not mistaken, whereas other approach is more precise as to what you cast to if you need a specific output data type.  I tend to use my approach as it is shorter to type / adjust from original query, but as I said the other approach is correct.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
###### Suggested Courses
Course of the Month15 days, 4 hours left to enroll

#### 840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.