Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Compute average without rounding

Posted on 2006-05-24
Medium Priority
Last Modified: 2012-05-05
I am trying to get the average number of items for all orders. With two decimal places.

SELECT CONVERT(DECIMAL(10,2),AVG(Items/Orders)) AvgItems
FROM table

I should get 2.77 as the result of dividing 333905 items by 120402 orders. I get 2.00.
I have tried SUM and AVG. What am I doing wrong?
Question by:Laura2112
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 800 total points
ID: 16756228
please try this:
SELECT CONVERT(DECIMAL(10,2),AVG(cast(Items as decimal(10,2))/cast(Orders as decimal(10,2)) )) AvgItems
FROM table

Author Comment

ID: 16756251
Right on!  Thanks!
LVL 34

Expert Comment

by:Brian Crowe
ID: 16756270
You actually only need to cast one of them

SELECT AVG(Items/CAST(Orders AS decimal(10,2)) AvgItems
FROM table

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

580 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