Solved

How do I count two columns in the same table and then calculate average, using MS SQL Server?

Posted on 2010-08-27
8
464 Views
Last Modified: 2012-05-10
How do I count two columns in the same table and then calculate average, using MS SQL Server?

"Approximate" syntax below, to show what I mean:

Select company, products as [average number of products]
Count products in first column.
Count Distinct companies in second column.
Average: total products/total Distinct companies
(which is average number of products for each distinct company).

I think I have to use Count and not Avg, but how do I combine this
for two columns (same table)?
0
Comment
Question by:hermesalpha
  • 4
  • 4
8 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33547918
select
      company,
      [average number of products] = isnull(Count(product)/nullif(Count(Distinct company),0),0),
      [Count Products] = Count(product),
      [Count Distinct company] = Count(Distinct company)
From tbl
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33547919
The nullif/isnull is there to prevent "divide by zero" error
If you can guarantee the table always has at least one company, you can simplify

select
      company,
      [average number of products] = Count(product)/Count(Distinct company)
      [Count Products] = Count(product),
      [Count Distinct company] = Count(Distinct company)
From tbl
0
 

Author Comment

by:hermesalpha
ID: 33548052
Can I add the condition that "only count if sold minimum 1,000 products to that company"? That is, if Count products for a company is below 1,000 products, don't include that company in the average result.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:hermesalpha
ID: 33548070
Where Count(sales.products) > 1000
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33548131
select
      Sum([Count Products]),
      [Count Distinct company] = count(company),
      [Average] = Sum([Count Products])/count(company)
from
(
select
      company,
      [Count Products] = Count(product)
From SALES
group by company
Having Count(product) >= 1000
) X
0
 

Author Comment

by:hermesalpha
ID: 33549478
I'm sorry but I was not clear about my query from the beginning. The query should only calculate the average of those products that have been sold 1,000 items of or more. So there only need to be a single row and single column in the result.

Select Avg(ProductID) As [Average sold products]
From Sales
Where Count(ProductID) > 1000
Group by ProductID
0
 

Author Comment

by:hermesalpha
ID: 33549483
Select Avg(Count(ProductID)) As [Average sold products]
From Sales
Where Count(ProductID) > 1000
Group by ProductID
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 500 total points
ID: 33550893
Which is exactly what my query gives you

-- this outer select sums up all the individual company counts (amont the >= 1000)
-- and counts the total rows (remember each row is one distinct company)
-- the average is
--     (sum of all sales to these companies) / (# of these companies)
select
      Sum([Count Products]),
      [Count Distinct company] = count(*),
      [Average] = Sum([Count Products])/count(company)
from
(
      -- this builds a subquery which lists companies, and for each commpany,
      -- the number of sales
      select company, [Count Products] = Count(product)
      From SALES
      group by company
      Having Count(product) >= 1000 -- remove rows where # of sales is less than 1000
      -- after removal, this subquery cotains a UNIQUE list of company
      -- having 1000 sales or more
) X
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

790 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