Solved

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

Posted on 2013-01-19
2
375 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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