Solved

SQL Query - get average dollar of an order withou using AVG()

Posted on 2013-01-19
2
366 Views
Last Modified: 2013-01-20
I am gettig the average dollar value of an Order.   Just need to double check if this query is missing anything since  

there are a certain number of orders in the ORDERS table, but not all of these orders are in the ORDERITEMS table.  

SELECT  SUM(ORDERITEMS.qty*INVENTORY.PRICE) / COUNT(ORDERS.ORDERID) AS 'Orders_Average'
 
  ,  SUM(ORDERITEMS.qty*INVENTORY.PRICE) / COUNT(ORDERITEMS.orderid)  AS 'OrderItems_Average'
 
 
FROM  ORDERITEMS LEFT OUTER JOIN
               INVENTORY ON ORDERITEMS.PARTID = INVENTORY.PARTID RIGHT OUTER JOIN
               ORDERS ON ORDERITEMS.ORDERID = ORDERS.ORDERID
0
Comment
Question by:ocdc
2 Comments
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 38797858
What is your desired outcome?  Your query has no problems if you are wanting just the 2 numbers for total sales amount divided by total number of rows in the orders table and total number of rows in the orderitems table.  If you are expecting something else, describe it a little more.
0
 
LVL 15

Accepted Solution

by:
gplana earned 500 total points
ID: 38797895
Your query is getting two values. The first one is the average related to the total of orders. The second one is the average related to orders with items.

How can an order have no item? Is this because the order includes also some services?
If this is the case, I would just add an entry on item table with the price by hour of the service, so you can the just put the number of hours on the qty attribute, and this simplyfy your query a lot.

Hope it helps.
0

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

743 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

10 Experts available now in Live!

Get 1:1 Help Now