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
 Displaying Average as a Decimal (Easy One)

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2011-06-17 : 17:44:07
Hi All - I have the following Table with results of 1s and 0s.

RECORD_NUMBER Q1
12654 1
12651 1
12657 0
12650 1

I am trying to display the average of the entire column in a two digit decimal format, so the above result would generate be ".75".

This code doesn't seem to work, as it only displays 1.00 or 0.00 any suggestions?

CAST (AVG ([Q1]) AS DECIMAL(5,2))

THANKS!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-06-17 : 17:54:39
Try: AVG(CAST [Q1] AS DECIMAL(5,2))
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-06-17 : 21:16:58
Why? The first rule of any tiered architecture is that all display formatting is done in the front end. Rob gave yuou the kludge, but you have more fundamental problems with SQL.

If this was a woodworking newsgroup and someone posted "What is the best kind of rocks to pound screws into fine furniture?" are you really helping them when you say "Granite! Use big hunks of granite!" I am the guy who replies with "Your question is bad. Don't you know about screwdrivers?" And I like to remind them that it takes six years to become a Journeyman Union Carpenter in New York State. Not Master, Journeyman.


--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -