Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value of a column from counting records in another table

Status
Not open for further replies.

marthirial

IS-IT--Management
Jan 29, 2005
20
US
SQL Server

I have two tables, one is "description" and the second is "comments" this two tables are related in the way that "description" can have many "comments".

I want to have a column in the table "description" that holds the count of "comments" belonging to such table, so I can display it as the count later on the page.

I have limited knowledge of Store Procedures or scripts in SQL , so an script would be greate for me to adapt.

Thanks
 
Personally, I would not suggest storing the count in the description table.

For every change in the comments table, the count needs to be updated, which is a clear overkill.

Instead create a view that gets you the count with other columns from description.
something like:
Code:
create view viewname as
select a.*, b.value
from   description a
       inner join 
       (select   keyfield,    
                 count(*) value
        from     comments 
        group by keyfield) b
       on (a.keyfield = b.keyfield)

Regards,
AA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top