Solved

losing precision in a calculated column

Posted on 2010-08-17
3
260 Views
Last Modified: 2012-05-10
The below query is an example of something I'm struggling with. It produces output like the following:

2052035      10      5      2
5036064      12      5      2
4016002      16      5      3
1010001      23      5      4
5031669      13      5      2
5031163      21      5      4

As you can see from the query, the last column is a calculated column which should give the 2nd column divided by the 3rd column.

I am trying to get the last column to give the exact DECIMAL amount, not an INTEGER. This is why I am trying to cast it etc.

How can I get the exact amount, ie. a decimal type here in this column, not an int ?

We are using SQL Server 2000
select EA.ex_pi_eval_answer_school_id, 
SUM(EA.ex_pi_eval_answer_response), 
COUNT(EA.ex_pi_eval_answer_response),
CAST(SUM(EA.ex_pi_eval_answer_response) / COUNT(EA.ex_pi_eval_answer_response) AS MONEY)

from tbl_ex_ProgramImpact_eval_answers EA

where EA.ex_pi_eval_answer_ProgramObjective_id = 1

group by EA.ex_pi_eval_answer_school_id

Open in new window

0
Comment
Question by:zorba111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33454940
try this, please
CAST( SUM(EA.ex_pi_eval_answer_response) as MONEY) / CAST(COUNT(EA.ex_pi_eval_answer_response) AS DECIMAL(20,4))

Open in new window

0
 

Author Comment

by:zorba111
ID: 33455011
Got it!!
This works... I remembered from somewhere that the default is to take the type of the first argument of any operator, and cast the result of an expression to that...

select EA.ex_pi_eval_answer_school_id,
SUM(CAST(EA.ex_pi_eval_answer_response AS DECIMAL)),
COUNT(EA.ex_pi_eval_answer_response),
SUM(CAST(EA.ex_pi_eval_answer_response AS DECIMAL)) / COUNT(EA.ex_pi_eval_answer_response)

from tbl_ex_ProgramImpact_eval_answers EA

where EA.ex_pi_eval_answer_ProgramObjective_id = 1

group by EA.ex_pi_eval_answer_school_id
0
 

Author Comment

by:zorba111
ID: 33455035
@angell

yours works too, also when simplified to:

SUM(EA.ex_pi_eval_answer_response) / CAST(COUNT(EA.ex_pi_eval_answer_response) AS DECIMAL(20,4))
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
query output (script) from a stored procedure 4 38
SQL Job Hung 17 37
SSIS GUID Variable 2 33
Change this SQL to get all nodes 3 38
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

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

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

Join & Ask a Question