Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ckuo@kahluadesigns.com
Yak Posting Veteran
58 Posts |
Posted - 2002-10-30 : 17:00:59
|
| Hi,Say I haveTABLEID---------Name-----Category1----------Test1----Car2----------Test2----Car3----------Test3----Car4----------Test4----Motorcycle5----------Test5----MotorcycleI would like to returnID---------Name-----Category-----CountCategory1----------Test1----Car----------32----------Test2----Car----------33----------Test3----Car----------34----------Test4----Motorcycle---25----------Test5----Motorcycle---2How would I get the CountCategory?Thanks |
|
|
tool
Starting Member
26 Posts |
Posted - 2002-10-30 : 17:36:35
|
| Here's two ways:SELECT ID, Name, Category, (SELECT COUNT(Category) FROM MyTable WHERE Category=t.Category) AS CountCategoryFROM MyTable tORSELECT t.ID, t.Name, t.Category, CountCategoryFROM MyTable t INNER JOIN (SELECT Category, COUNT(Category) AS CountCategory FROM MyTable GROUP BY Category) AS cON t.Category = c.Category |
 |
|
|
|
|
|