?
Solved

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

Posted on 2013-01-19
2
Medium Priority
?
387 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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