Compute average without rounding

Posted on 2006-05-24
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 142

    Accepted Solution

    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

    Right on!  Thanks!
    LVL 34

    Expert Comment

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

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

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now