# Query question

Hello,

I have a table that has the following columns: CountryCode, ClinicCode, Month, NumberOfPatients, AverageHeight

It holds the average height for the patients that have visited a clinic during a given month.

Example data:
CountryCode, ClinicCode, Month, NumberOfPatients, AverageHeight
10, 1, 2012-01-01, 10, 1.78
10, 1, 2012-02-01, 8, 1.77
10, 1, 2012-03-01, 12, 1.75
10, 2, 2012-01-01, 7, 1.74
10, 2, 2012-02-01, 6, 1.79
10, 2, 2012-03-01, 1, 1.95
11, 3, 2012-01-01, 10, 1.78
11, 3, 2012-02-01, 20, 1.82
11, 3, 2012-03-01, 11, 1.75
11, 4, 2012-01-01, 4, 1.75
11, 4, 2012-02-01, 16, 1.81
11, 4, 2012-03-01, 4, 1.84

I now need to create average statistics per County using the CountyCode column.

Since I know the number of patients involved in each average at each clinic I can work out the total height for a county and the total number of patients.  The average is then a simple divide of these two totals.

However I am not sure how the query should look:
Select
CountCode,
Month,
“something” as CountryAverage
From MyTable
Group by CountyCode, Month

How do I get the sum of the AverageHeight, and total number of patients per CountryCode and Month.

Is it sum( AverageHeight * NumberOfPatients) / Sum(NumberOfPatients) ?

Is it really that simple?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Billing EngineerCommented:
yes, that simple.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

MSSQL Senior EngineerCommented:
Your question isn't about T-SQL but statistical formula.
I think it's how you told. Keep it simple ;)
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.