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

  • Home
  • SEARCH
  • 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 7738549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:17:41+00:00 2026-06-01T08:17:41+00:00

I have an MS SQL Server 2008 database where I store places that serve

  • 0

I have an MS SQL Server 2008 database where I store places that serve food (cafés, restaurants, diners etc.). On a web site connected to this database people can rate the places on a scale from 1 to 3.

On the web site there’s a page where people can view a top list with the top 25 (best rated) places in a certain city. The database structure looks something like this (there is more info stored in the tables, but here’s the relevant info):
Database structure: Cities->Places->Votes

A place is situated in a city and votes are placed on a place.

Up until now I’ve just calculated an average vote score for each place where I divide the sum of all votes for a certain place with the number of votes for that place, something like this (pseudo code):

vote_count = total number of votes for the place
vote_sum = total sum of all the votes for the place

vote_score = vote_sum/vote_count

I also have to handle divide by zero if a place has no votes. All this is done inside the stored procedure that fetches the other data that I want to display in the top list. Here is the current stored procedure that fetches the top 25 places with the highest vote score:

ALTER PROCEDURE [dbo].[GetTopListByCity]
    (
    @city_id Int
    )
AS
    SELECT TOP 25 dbo.Places.place_id, 
           dbo.Places.city_id,
           dbo.Places.place_name,
           dbo.Places.place_alias,
           dbo.Places.place_street_address,
           dbo.Places.place_street_number,
           dbo.Places.place_zip_code,
           dbo.Cities.city_name,
           dbo.Cities.city_alias,
           dbo.Places.place_phone,
           dbo.Places.place_lat,
           dbo.Places.place_lng,
           ISNULL(SUM(dbo.Votes.vote_score),0) AS vote_sum,
           (SELECT COUNT(*) FROM dbo.Votes WHERE dbo.Votes.place_id = dbo.Places.place_id) AS vote_count,
           COALESCE((CONVERT(FLOAT,SUM(dbo.Votes.vote_score))/(CONVERT(FLOAT,(SELECT COUNT(*) FROM dbo.Votes WHERE dbo.Votes.place_id = dbo.Places.place_id)))),0) AS vote_score

    FROM dbo.Places INNER JOIN dbo.Cities ON dbo.Places.city_id = dbo.Cities.city_id
    LEFT OUTER JOIN dbo.Votes ON dbo.Places.place_id = dbo.Votes.place_id
    WHERE dbo.Places.city_id = @city_id
    AND dbo.Places.hidden = 0
    GROUP BY dbo.Places.place_id,
             dbo.Places.city_id,
             dbo.Places.place_name,
             dbo.Places.place_alias,
             dbo.Places.place_street_address,
             dbo.Places.place_street_number,
             dbo.Places.place_zip_code,
             dbo.Cities.city_name,
             dbo.Cities.city_alias,
             dbo.Places.place_phone,
             dbo.Places.place_lat,
             dbo.Places.place_lng
    ORDER BY vote_score DESC, vote_count DESC, place_name ASC

    RETURN

As you can see it fetches more than just the vote score – I need data about the place, the city it’s situated in and so on. This works fine, but there is one big problem: the vote score is too simple because it doesn’t take in to account the number of votes. With the simple calculation method a place that has one vote with the score 3 will end up higher in the list than a place that has fourteen votes with the score 3 and one vote with the score 2:

3/1 = 3
(14*3 + 1*2) = 44/15 = 2.933333333333

To fix this I’ve been looking into using some form of weighted average/weighted index. I’ve found an example of a true bayesian estimate that looks promising. It looks like this:

weighted rating (WR) = (v ÷ (v+m)) × R + (m ÷ (v+m)) × C

where:

R = average for the place (mean) = (Rating)
v = number of votes for the place = (votes)
m = minimum number of votes required to be listed in the Top 25 (unsure how many, but somewhere between 2-5 seems realistic)
C = the mean vote across the whole database

The problems begin when I try to implement this weighted rating in a stored procedure – it quickly becomes complicated and I get tangled into parenthesis and loose track of what the stored procedure does.

Now I need some help with two questions:

Is this a suitable method for calculating a weighted index for my site?

How would this (or another suitable calculation method) look like when implemented in a stored procedure?

  • 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-06-01T08:17:42+00:00Added an answer on June 1, 2026 at 8:17 am

    I cannot see any problem with you calculations. But I can see that you are doing the same thing many times. My suggestion will help you do the aggregates in one place and then the select is quite easy.

    ;WITH CTE
    (
        SELECT
            SUM(dbo.Votes.vote_score) AS SumOfVoteScore,
            COUNT(*) AS CountOfVotes,
            Votes.place_id
        FROM
            Votes
        GROUP BY
            Votes.place_id
    )
     SELECT TOP 25 
        dbo.Places.place_id, 
        dbo.Places.city_id,
        dbo.Places.place_name,
        dbo.Places.place_alias,
        dbo.Places.place_street_address,
        dbo.Places.place_street_number,
        dbo.Places.place_zip_code,
        dbo.Cities.city_name,
        dbo.Cities.city_alias,
        dbo.Places.place_phone,
        dbo.Places.place_lat,
        dbo.Places.place_lng,
        ISNULL(CTE.SumOfVoteScore,0) AS vote_sum,
        CTE.CountOfVotes AS vote_count,
        COALESCE((CONVERT(FLOAT,CTE.SumOfVoteScore)/
        (CONVERT(FLOAT,CTE.CountOfVotes))),0) AS vote_score
    
    FROM dbo.Places INNER JOIN dbo.Cities ON dbo.Places.city_id = dbo.Cities.city_id
    LEFT JOIN CTE ON dbo.Places.place_id=CTE.place_id
    WHERE dbo.Places.city_id = @city_id
    AND dbo.Places.hidden = 0
    GROUP BY dbo.Places.place_id,
             dbo.Places.city_id,
             dbo.Places.place_name,
             dbo.Places.place_alias,
             dbo.Places.place_street_address,
             dbo.Places.place_street_number,
             dbo.Places.place_zip_code,
             dbo.Cities.city_name,
             dbo.Cities.city_alias,
             dbo.Places.place_phone,
             dbo.Places.place_lat,
             dbo.Places.place_lng
    ORDER BY vote_score DESC, vote_count DESC, place_name ASC
    

    The CTE function helps us reuse the calculations. So that we don’t have to use SUM(vote_score) and SELECT COUNT(*) FROM Votes WHERE... multiples times. So then when you are selecting the calculations is quite easy to follow.

    I hope this helps

    Edit

    You do not have to define the table columns in the CTE. This CTE (SumOfVoteScore, CountOfVotes, place_id) AS works as good as this CTE AS. You need to define the columns if you are using a recursive cte. Beacuse you are union with the other part.

    For reference here and here you will find some information about CTE functions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a SQL Server 2008 database that has stored procedures to handle reads/writes/etc.
I have an application that accesses a sql server 2008 database. The server and
We have a SQL Server 2008 R2 Enterprise a database that is populated with
I have a load of records to store in a SQL Server 2008 database.
I have a database (SQL server express 2008) which has a column that is
If I have a SQL Server 2008 database that uses FILESTREAM and stores 10
I'm helping implement a SQL Server 2008 database and I have several columns that
I have a SmallDateTime field in my Sql Server 2008 database to store users
I have a SQL Server 2008 database where I would like to be able
I have an SQL Server 2008 database with a column of type geography storing

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.