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

x
Solved

# Compute average without rounding

Posted on 2006-05-24
Medium Priority
299 Views
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?
0
Question by:Laura2112

LVL 143

Accepted Solution

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

Author Comment

ID: 16756251
Right on!  Thanks!
0

LVL 34

Expert Comment

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

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

## Featured Post

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
Course of the Month13 days, 7 hours left to enroll