I have below tables:
ProductsTable (has product details),
UsersTable (has all application users),
RatingCategry (category for which user can rate a products like Quality, Cost etc.) and
UserRatingsforProductsTable (Users rating for a product for any RatingCategory) for a Mobile Application.
Products (ProductId, ProductName… other details like cost etc.)
UsersTable (UserId, UserName… other details like DateOfJoin etc.)
RatingCategory (RatingCategoryId, CategoryName)
**Data:**
1 Quality
2 Cost
UserRatingsforProductsTable (RatingId, UserId, ProductId, RatingCategoryId, Score)
**Data:**
1 5006 20 1 4.5
2 5006 20 2 5.0
3 5600 23 1 3.5
4 7800 20 1 2.5
5 8900 34 1 4.0
6 8900 34 2 4.5
What will be the best and faster query for a Mobile Application to show “Rating ScoreTotal” in the Products list (ProductId, ProductName, Cost, RatingScore)? Shall I do calculation in the Query or Add a new column in Products table that will be auto updated when each Rating added into UserRatingsforProductsTable?
The expected output should be:
ProductId, ProductName, Cost, OverallRatingForThisProd
--------- ----------- ----- ------------------------
*** OverAllRating = Average of Ratings from All Categories for the Specific Product from UserRatingsforProductsTable.
If the answer will be Query instead of adding a Column, can you please advise an optimized query?
Thank you.
Best Answer
I don't think adding a new column will be efficiently and you'll need to do more things like adding
TRIGGERs
to update the field and you'll get the newColumnvalue in every row for the tableUserRatingsForProductsTable
.Information:
Query:
Try it on SQLFiddle.