Help with query against AdventureWorks database - Average Sale Price

Posted on 2010-08-28
Last Modified: 2012-05-10
Need a hand with this query:

Return the average Sale Price by Product Category

Question by:John500
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
LVL 60

Expert Comment

by:Kevin Cross
ID: 33551302
I notice you have a number of these questions, so I will show you this one in generic terms to ensure that you get the understanding of what to do here.

Your sales table has:


Your product table has:


Your category table has:


What you will need to do is figure out whether you want ALL product categories regardless of having parts assigned and/or having sales.  If you want to have all categories you would use a LEFT JOIN, if only those with sales (INNER) JOIN.

For the product and sales data we will use a JOIN for both.  Let's start with that:

   select p.categoryid, avg(s.salesprice) as avgsalesprice
   product p
   inner join sales s on p.productid = s.productid
   group by p.categoryid

What this gives you is your average sales by category id.

Now we apply our category table to it:

select c.categoryname
     , coalesce(sls.avgsalesprice, 0.00) as avgsalesprice
from category c
left join (
   select p.categoryid, avg(s.salesprice) as avgsalesprice
   product p
   inner join sales s on p.productid = s.productid
   group by p.categoryid
) sls on c.categoryId = sls.categoryId

What we have done here is to put our previous query as a derived table with alias "sls" and then joined on the categoryid.  Now we can display a category name and its average sales price or 0 using coalesce to filter nulls or no sales.

Hope that helps.


Author Comment

ID: 33551361

Yep, that helps.  Appreicate that.

Out of curiosity, do you feel you have the latest version of the database:

LVL 60

Expert Comment

by:Kevin Cross
ID: 33551369
My code above was pseudo code and not meant to be the actual structure from the codeplex. As stated above, I realized after answering a couple that it would be better to teach you the concept than write code for you. Take the information from above and try your hand at the code against the AdventureWorks tables yourself.

If you have difficulty, then post back here the query you have constructed and what issues you are having with it.
LVL 51

Accepted Solution

Mark Wills earned 500 total points
ID: 33561297
Hi John500

Interesting series of questions...

You really need to be getting "inside" that Adventureworks database. For example, the "product" scenarios :  (use the drop down at the top to select SQL2005 or SQL2008 etc) and the data dictionary :

Your question is quite ambiguous, not sure if you need help with understanding the data links or the SQL involved to achieve an average. Then, not sure if it is the average unit sales price (ie after discounts) or the average sales value being qty * (unit price - unit discount )...

And given the warning above, we normally need a hint as to where your difficulties are and that way we can help educate and clear up any specific problems you are having.

Anyway, have a look at this sample query from Adventureworks - it should go close, but you will need to modify a bit to take into account the specific requirements of your question...

SELECT PC.Name AS Category, PSC.Name AS Subcategory, P.ProductNumber, P.Name AS Product, avg(isnull(UnitPrice,0.00) - isnull(UnitPriceDiscount,0.00)) as Average_Unit_SalesPrice
FROM Production.Product AS P
INNER JOIN Production.ProductSubcategory AS PSC ON PSC.ProductSubcategoryID = P.ProductSubcategoryID
INNER JOIN Production.ProductCategory AS PC ON PC.ProductCategoryID = PSC.ProductCategoryID
INNER JOIN Sales.SalesOrderDetail as SD ON SD.ProductID = P.ProductID
Group By PC.Name, PSC.Name, P.ProductNumber, P.Name
Order by 1,2,3,4


Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

697 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