Jump to content

Calculating a rating by adding number of points and dividing by number of items


Recommended Posts

I have a site that users can post links to files to download. They can rate these files on a 1-5 scale. I had someone make a Top Rated section for the site, to show the user with the highest rating for their links. However, it just adds a total of points, i.e. if the user has 3 links rated 5/5, their points are 15, if a user has 10 links rated 5/5, their points are 50. It displays the rating based on the number of points, but I would like it to divide the number of points by the number of links. Here is the section of code that deals with the rating calculation, I just don't know enough about syntax to figure out how to divide by the other variable.

 

<?xml version="1.0" encoding="utf-8"?>
<sqlmap>
  <select id="getTopPoster" resultClass="array">
    <![CDATA[
   select username, count(linkid) as linkposted from link group by username order by linkposted desc limit 5
   ]]>
  </select>

  <select id="getTopRated" resultClass="array">
    <![CDATA[
   select username, sum(rate) as point from link where rate<>-1 group by username order by point desc limit 5
   ]]>
  </select>
</sqlmap>

I know it is xml code, but I didn't know where else to put it, the rest of the site is built using Prado.

Try replacing the second query with this:

 

select username, sum(rate)/COUNT(username) as point from link where rate<>-1 group by username order by point desc limit 5

 

That worked perfectly, thanks a lot!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.