Help with query against AdventureWorks database - Average Sale Price

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

Return the average Sale Price by Product Category

Question by:John500
  • 2
LVL 61

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 61

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 2000 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 : 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


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

616 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