Average and count at end of existing table

jpunja

New Member
Joined
Dec 29, 2009
Messages
8
Although I know this is not how database-tables should be used, I would like to add two columns (i.e. average and count) to my existing table. The original table:

NameAgeCity
John10NY
John80London
John30NY
Bill10NY
Bill40NY
Bill25NY

<tbody>
</tbody>

I would like to add two columns:

  • average of age group by name and CITY = NY
  • Count: group by name and CITY = NY

So you get:

NameAgeCitycountaverage
John10NY220
John80London--
John30NY220
Bill10NY325
Bill40NY325
Bill25NY325

<tbody>
</tbody>


I can make a table with average and count, and join this table in a second query. But I can't get it into one query (what i realy need). Hopefully you have the solution!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Cross-posted here: Add [count] and [average] to existing table

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule #13 here: Forum Rules).

This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.

For a more complete explanation on cross-posting, see here: Excelguru Help Site - A message to forum cross posters).
 
Upvote 0
You can do it all in a single query using a subquery, i.e.
Code:
SELECT TableName.Name, TableName.Age, TableName.City, SubQuery.CountOfAge, SubQuery.AvgOfAge
FROM TableName 
INNER JOIN 
[COLOR=#0000ff](SELECT TableName.Name, TableName.City, Count(TableName.Age) AS CountOfAge, Avg(TableName.Age) AS AvgOfAge
FROM TableName
GROUP BY TableName.Name, TableName.City) as SubQuery[/COLOR]
ON (TableName.City = SubQuery.City) AND (TableName.Name = SubQuery.Name);
 
Upvote 0

Forum statistics

Threads
1,221,814
Messages
6,162,132
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top