?
Solved

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

Posted on 2013-01-19
2
Medium Priority
?
386 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 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