• Status: Solved
• Priority: Medium
• Security: Public
• Views: 503

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

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
hermesalpha
• 4
• 4
2 Solutions

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

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

Author Commented:
Where Count(sales.products) > 1000
0

Commented:
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 Commented:
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 Commented:
Select Avg(Count(ProductID)) As [Average sold products]
From Sales
Where Count(ProductID) > 1000
Group by ProductID
0

Commented:
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
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.