• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 986
  • Last Modified:

Help with query against AdventureWorks database - Average Sale Price

Need a hand with this query:

Return the average Sale Price by Product Category

  • 2
1 Solution
Kevin CrossChief Technology OfficerCommented:
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.

John500Author Commented:

Yep, that helps.  Appreicate that.

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


Kevin CrossChief Technology OfficerCommented:
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.
Mark WillsTopic AdvisorCommented:
Hi John500

Interesting series of questions...

You really need to be getting "inside" that Adventureworks database. For example, the "product" scenarios : http://msdn.microsoft.com/en-us/library/ms124670(v=SQL.90).aspx  (use the drop down at the top to select SQL2005 or SQL2008 etc) and the data dictionary : http://msdn.microsoft.com/en-US/library/ms124438(v=SQL.90).aspx

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now