Question : SQL Count Query In a Main Query

I have a Categories table. The table has an "ID", "Name" and "ID_Bond" field. It is like an endless tree. Every row is bound to another row with the ID_Bond field, it contains the ID of the main rows ID field.

I have a query like SELECT [Name] FROM Categories WHERE ID_Bond = 1. This returns all the sub rows of the row with ID=1.

My question is I need the sub categories count for each row that the query returns.

Something like SELECT [Name], (....) as _TotalSubCategoriesCount FROM Categories WHERE ID_Bond = 1.

Answer : SQL Count Query In a Main Query

igni7e,

Sorry to say, but you are wrong.

Name                             TotalSubCategoriesCount
-------------------------------- -----------------------
Games                            1
FPS                              2
Strategy                         2
Doom                             2
HL                               2
StarCraft                        1

(6 row(s) affected)

that's the result of your query
and your query after my modifications

Name                             SubCategoryCount
-------------------------------- ----------------
FPS                              2
Strategy                         1

(2 row(s) affected)

The mistake you made is to compare C1.ID_Bond to C2.ID_Bond
rather than C2.ID_Bond to C1.ID

Random Solutions  
 
programming4us programming4us