Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6227963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:18:28+00:00 2026-05-24T09:18:28+00:00

I have to related tables like in the example script below: — Creating table

  • 0

I have to related tables like in the example script below:

-- Creating table 'Product'
CREATE TABLE [dbo].[Product] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Text] nvarchar(max)  NOT NULL,
    [AvgRating] decimal(18,2)  NOT NULL
);
GO

-- Creating table 'Review'
CREATE TABLE [dbo].[Review] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Text] nvarchar(max)  NOT NULL,
    [Rating] decimal(18,2)  NOT NULL,
    [Product_Id] int  NOT NULL
);
GO

-- Creating primary key on [Id] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [PK_Product]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Review'
ALTER TABLE [dbo].[Review]
ADD CONSTRAINT [PK_Review]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating foreign key on [Product_Id] in table 'Review'
ALTER TABLE [dbo].[Review]
ADD CONSTRAINT [FK_ProductReview]
    FOREIGN KEY ([Product_Id])
    REFERENCES [dbo].[Product]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductReview'
CREATE INDEX [IX_FK_ProductReview]
ON [dbo].[Review]
    ([Product_Id]);
GO

I would like to compute AvgRating on Product row when user inserts/updates/deletes review. The most obvious and brute force approach that comes to my mind is to pull data from database and compute the average on client side, and then update the product row manually. Is it possible to do it automatically on database server without having to pull the data? If so, how? Database is hosted on MS SQL Server 2008.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:18:29+00:00Added an answer on May 24, 2026 at 9:18 am

    You could create a trigger on the Review table. After an update on Review it could recompute the average review.

    CREATE TRIGGER TRG_REVIEW 
    ON Review
    AFTER INSERT, UPDATE, DELETE
    AS 
    
    ;WITH ratings 
    AS 
    (
        SELECT product_id, AVG(Rating) AS rating
        FROM Review R
        WHERE EXISTS (SELECT * FROM INSERTED WHERE product_id = R.product_id AND UPDATE(rating))
             OR EXISTS (SELECT * FROM DELETED WHERE product_id = R.product_id)
        GROUP BY product_id
    )
    UPDATE P set AvgRating = COALESCE(R.rating,0)
    FROM Product P 
    INNER JOIN ratings R 
    ON P.id = R.Product_id 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help. I have a sql table t2 related to two other tables
I have a long running insert transaction that inserts data into several related tables.
I have several related CVS projects, and I'd like to move them into subdirectories
While related questions have been asked before I would like to see an idea
I have a script that appends some rows to a table. One of the
I have a snippet to create a 'Like' button for our news site: <iframe
in my db I have two tables that are related. Lets call them Job
I have two tables whichs relation is expressed in a third n:m table. Lets
I have a set of tables that I would like to build a view
I have two related classes which share a common interface and are both stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.